Skip to content

Error related with JPA EntityGraph in Spring Boot 3.4.0 #3709

@dimcookies

Description

@dimcookies

Summary:
I originally posted this as a Spring Boot issue, but since this is JPA/Hibernate related, maybe it belongs here.
After upgrading to Spring Boot 3.4.0 (Hibernate 6.6.2-Final), 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 Spring Boot 3.3.5 (Hibernate 6.5.3-Final). As it is mentioned in the original issue, this has to do with using non-managed objects but it is almost impossible to identify all the cases, so i trying to check if there is a way to get the functionality in the previous version (without downgrading hibernate)

Details:
TestController calls addPayment from TestService1. This method is transactional.
This method calls the save() method of a repository
Then it calls another service TestService2 method2

This method performs a query using a repository and then another query from another repository which uses EntityGraph.

The exception is

2024-12-09T19:04:46.304+02:00 ERROR 24764 --- [errorCheck] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.spi.EntityEntry.getMaybeLazySet()" because "entityEntry" is null] with root cause

java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.spi.EntityEntry.getMaybeLazySet()" because "entityEntry" is null
	at org.hibernate.sql.results.graph.entity.internal.EntityInitializerImpl.resolveInstanceSubInitializers(EntityInitializerImpl.java:625) ~[hibernate-core-6.6.2.Final.jar:6.6.2.Final]
	at org.hibernate.sql.results.graph.entity.internal.EntityInitializerImpl.resolveInstance(EntityInitializerImpl.java:988) ~[hibernate-core-6.6.2.Final.jar:6.6.2.Final]
	at org.hibernate.sql.results.graph.entity.internal.EntityInitializerImpl.resolveInstance(EntityInitializerImpl.java:97) ~[hibernate-core-6.6.2.Final.jar:6.6.2.Final]

While trying to reproduce the issue i got another exception, but not constantly

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

If we change the version of Spring Boot to 3.3.5, this works

Steps to reproduce:

  1. Run the attached project using postgres as database.
    errorCheck.zip

  2. Access "http://localhost:8080/test" and an exception will be produced

2024-12-09T19:04:46.304+02:00 ERROR 24764 --- [errorCheck] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.spi.EntityEntry.getMaybeLazySet()" because "entityEntry" is null] with root cause

If we change the Spring Boot version to 3.3.5 the endpoint will return the response "1"

Environment:
Spring Boot version: 3.4.0
Java version: 21
Operating System: Windows 11

Attachments
Sample project to reproduce the error

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: external-projectFor an external project and not something we can fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions