spring-projects / spring-projects/spring-framework
LazyInitializationException with GraalVM Native Image in Spring Boot 3.3.5 Application
@sdeleuze is already working on this.
Since Jun 10, 2025.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.