Skip to content

Commit

Permalink
Merge pull request #459 from dreab8/hibernateFlatClassLoaderService
Browse files Browse the repository at this point in the history
Hibernate ORM - Hql queries trying to fetch an association cause NPE
  • Loading branch information
gsmet authored Jan 10, 2019
2 parents 4de295b + bb01376 commit e1519e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.hibernate.AssertionFailure;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;

Expand All @@ -49,10 +50,9 @@ public <T> Class<T> classForName(String className) {
try {
return (Class<T>) Class.forName( className, false, getClassLoader() );
}
catch (ClassNotFoundException e) {
log.debugf( "Could not load class '%s' using Class.forName(String) and class loader %s", className , getClassLoader());
catch (Exception | LinkageError e) {
throw new ClassLoadingException( "Unable to load class [" + className + "]", e );
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,35 @@ private static void doStuffWithHibernate(EntityManagerFactory entityManagerFacto
//Try a JPA named query:
verifyJPANamedQuery(entityManagerFactory);

verifyHqlFetch(entityManagerFactory);

deleteAllPerson(entityManagerFactory);

}

private static void verifyHqlFetch(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
try {
EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();

em.createQuery( "from Person p left join fetch p.address a" ).getResultList();

transaction.commit();
}
catch (Exception e) {
if ( transaction.isActive() ) {
transaction.rollback();
}
throw e;
}
}
finally {
em.close();
}
}

private static void verifyJPANamedQuery(final EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
Expand Down

0 comments on commit e1519e8

Please sign in to comment.