Skip to content

Commit

Permalink
Provide actionable information when EntityManager is not available in…
Browse files Browse the repository at this point in the history
… Hibernate Panache

Fixes: quarkusio#13324
Fixes: quarkusio#13338
  • Loading branch information
geoand committed Nov 17, 2020
1 parent 03d0fd6 commit d55744d
Showing 1 changed file with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<EntityManager> 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<EntityManager> 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() {
Expand Down

0 comments on commit d55744d

Please sign in to comment.