-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Issue Summary
In a Spring Boot 3.3.5 application using GraalVM (22.3.0) for native image compilation, the following exception occurs when attempting to initialize lazy-loaded Hibernate entities:
org.springframework.security.authentication.InternalAuthenticationServiceException:
Unable to perform requested lazy initialization
[io.github.susimsek.springnextjssamples.entity.RoleEntity.name] - session is closed and settings disallow loading outside the Session
This issue occurs only in the GraalVM Native Image environment. The error does not appear when running the application in a standard JVM environment.
Project Repository
- Repository: https://github.com/susimsek/spring-graalvm-demo
Environment Details
- Java Version: 21
- Spring Boot Version: 3.3.5
- GraalVM Version: 22.3.0
- Hibernate Version: 6.5.3.Final
Steps to Reproduce
-
Clone the repository spring-graalvm-demo.
-
Configure the project for native image generation with GraalVM.
-
Run the native image build with the following command:
./mvnw native:compile -Pnative
-
When accessing endpoints that require lazy initialization of Hibernate entities, observe the
LazyInitializationException
.
Analysis
The exception suggests that the Hibernate session is closed before the lazy-loaded entity (RoleEntity.name
) can be accessed, causing a LazyInitializationException
. This may indicate that GraalVM's native image environment is not handling Hibernate's lazy loading proxy as expected, particularly under Spring Security's context.
Related Configuration
The application uses hibernate-enhance-maven-plugin
to enable lazy loading and other entity enhancements:
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<id>enhance</id>
<configuration>
<enableLazyInitialization>true</enableLazyInitialization>
<enableDirtyTracking>true</enableDirtyTracking>
<enableAssociationManagement>true</enableAssociationManagement>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
Expected Behavior
The application should be able to lazily load entity fields in the GraalVM native environment as it does in a standard JVM environment without throwing a LazyInitializationException
.
Additional Notes
- The issue appears to be specific to GraalVM's native image behavior with Hibernate and Spring Security, particularly with lazy initialization outside an active session.