Error related with JPA EntityGraph in Hibernate 6.6.X

Hi all,

my issue was original posted to Spring Boot issues, but it seems to be hibernate related.
It seems that after upgrading to Hibernate 6.6.2-Final (part of spring boot 3.4), there is a Hibernate related error when a transactional method, after saving a row, tries to retrieve some data from a repository method using EntityGraph. This worked in Hibernate 6.5.3-Final (part of spring boot 3.3)

I have a small spring boot project to demonstrate the error

The error i am getting is

java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.spi.EntityEntry.getMaybeLazySet()" because "entityEntry" is null

and also

an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: possible non-threadsafe access to the session

As other people mentioned in the original post, this might have to do with using objects that are not created/managed by hibernate. Since i can not identify all such cases and fix them, i am wondering if there is something else i can do (apart from downgrading Hibernate to the version that this works)

Thank you in advance for your time

Here is also some JPA code to reproduce the issue (as provided as a reply in the reported issue in Spring Data)


long userId = 1;

Payment payment = new Payment();

var invoice = new Invoice();
invoice.setId(1L);
payment.setInvoice(invoice);

AppUser appUser = new AppUser();
appUser.setId(userId);
payment.setCreatedFrom(appUser);

entityManager.persist(payment);

Invoice invoiceLoaded = (Invoice) entityManager.createQuery("SELECT i FROM Invoice i WHERE i.id = ?1")
		.setParameter(1, invoice.getId()).getSingleResult();

jakarta.persistence.EntityGraph<StudentFeesToInvoice> entityGraph = entityManager
		.createEntityGraph(StudentFeesToInvoice.class);
entityGraph.addSubgraph("invoice")
		.addSubgraph("payments")
		.addAttributeNodes("createdFrom");

// failure happens here
entityManager.createQuery("SELECT s FROM StudentFeesToInvoice s WHERE id = ?1")
		.setHint("jakarta.persistence.fetchgraph", entityGraph)
		.setParameter(1, invoiceLoaded.getId()).getResultList();

Since you seem to have some code which reproduces the issue using only Hibernate (JPA), I would suggest isolating that in a simple reproducer test case and creating a new bug report in our issue tracker.