diff --git a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java index 3730ee6a65fec4..7b8acfbbc8d1a3 100644 --- a/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java +++ b/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java @@ -15,7 +15,11 @@ import javax.transaction.SystemException; import javax.transaction.TransactionManager; +import io.agroal.api.AgroalDataSource; +import io.quarkus.agroal.DataSource; import io.quarkus.arc.Arc; +import io.quarkus.arc.ArcContainer; +import io.quarkus.arc.InstanceHandle; import io.quarkus.hibernate.orm.PersistenceUnit; import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil; import io.quarkus.panache.common.Parameters; @@ -48,13 +52,35 @@ public static EntityManager getEntityManager(Class clazz) { } public static EntityManager getEntityManager(String persistentUnitName) { + ArcContainer arcContainer = Arc.container(); if (persistentUnitName == null || PersistenceUnitUtil.isDefaultPersistenceUnit(persistentUnitName)) { - return Arc.container().instance(EntityManager.class).get(); + InstanceHandle emHandle = arcContainer.instance(EntityManager.class); + if (emHandle.isAvailable()) { + return emHandle.get(); + } + if (!arcContainer.instance(AgroalDataSource.class).isAvailable()) { + throw new IllegalStateException( + "The default datasource has not been properly configured. See https://quarkus.io/guides/datasource#jdbc-datasource for information on how to do that."); + } + throw new IllegalStateException( + "No entities were found. Did you forget to annotate your Panache Entity classes with '@Entity'?"); } - PersistenceUnit.PersistenceUnitLiteral persistenceUnitLiteral = new PersistenceUnit.PersistenceUnitLiteral( - persistentUnitName); - return Arc.container().instance(EntityManager.class, persistenceUnitLiteral).get(); + InstanceHandle emHandle = arcContainer.instance(EntityManager.class, + new PersistenceUnit.PersistenceUnitLiteral(persistentUnitName)); + if (emHandle.isAvailable()) { + return emHandle.get(); + } + if (!arcContainer.instance(AgroalDataSource.class, + new DataSource.DataSourceLiteral(persistentUnitName)).isAvailable()) { + throw new IllegalStateException( + "The named datasource '" + persistentUnitName + + "' has not been properly configured. See https://quarkus.io/guides/datasource#multiple-datasources for information on how to do that."); + } + throw new IllegalStateException( + "No entities were attached to persistence unit '" + persistentUnitName + + "'. Did you forget to annotate your Panache Entity classes with '@Entity' or improperly configure the 'quarkus.hibernate-orm.\" " + + persistentUnitName + "\".packages' property?"); } public static EntityManager getEntityManager() {