Skip to content

LazyInitializationException with GraalVM Native Image in Spring Boot 3.3.5 Application #33848

@susimsek

Description

@susimsek

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

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

  1. Clone the repository spring-graalvm-demo.

  2. Configure the project for native image generation with GraalVM.

  3. Run the native image build with the following command:

    ./mvnw native:compile -Pnative
  4. 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.

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)status: waiting-for-triageAn issue we've not yet triaged or decided ontheme: aotAn issue related to Ahead-of-time processing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions