2

I am using spring boot 3:

     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>3.0.2</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>

And I want to generate a native image using buildpacks but I have an error related to hibernate and hibernate-enhance-maven-plugin. From what I understand, this plugin should generate code at buildtime but it doesn't seem to be working. My pom is:

     <build>
         <plugins>
             <plugin>
                 <groupId>org.hibernate.orm.tooling</groupId>
                 <artifactId>hibernate-enhance-maven-plugin</artifactId>
                 <version>${hibernate.version}</version>
                 <executions>
                     <execution>
                         <configuration>
                             <failOnError>true</failOnError>
                             <enableLazyInitialization>true</enableLazyInitialization>
                             <enableDirtyTracking>true</enableDirtyTracking>
                             <enableAssociationManagement>true</enableAssociationManagement>
                             <enableExtendedEnhancement>false</enableExtendedEnhancement>
                         </configuration>
                         <goals>
                             <goal>enhance</goal>
                         </goals>
                     </execution>
                 </executions>
             </plugin>
             <plugin>
                 <groupId>org.graalvm.buildtools</groupId>
                 <artifactId>native-maven-plugin</artifactId>
             </plugin>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                     <image>
                         <env>
                             <BP_JVM_VERSION>17</BP_JVM_VERSION>
                         </env>
                     </image>
                 </configuration>
             </plugin>
         </plugin>
     </build>

when I run mvn -Pnative spring-boot:build-image I can see in the console:

[INFO] --- hibernate-enhance-maven-plugin:6.1.5.Final:enhance (default) @ medea-core ---
[INFO] Starting Hibernate enhancement for classes on /home/tronxi/workspace/medea/medea-core/target/classes
Feb 15, 2023 11:54:10 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.1.5.Final

so it looks like the plugin is running, however when I make a request involving a lazy entity I get this error

2023-02-15T23:04:05.335Z INFO 1 --- [nio-8080-exec-3] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command

org.hibernate.HibernateException: Generation of HibernateProxy instances at runtime is not allowed when the configured BytecodeProvider is 'none'; Your model requires a more advanced BytecodeProvider to be enabled.
at org.hibernate.bytecode.internal.none.DisallowedProxyFactory.getProxy(DisallowedProxyFactory.java:34) ~[na:na]
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4935) ~[com.medeamind.medeacore.MedeaCoreApplication:6.1.5.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.createProxy(DefaultLoadEventListener.java:465) ~[na:na]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:318) ~[na:na]
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:113) ~[na:na]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75) ~[na:na]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:118) ~[com.medeamind.medeacore.MedeaCoreApplication:6.1.5.Final]

In case it can help this is my complete pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.name</groupId>
    <artifactId>name</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>name</name>
    <description>name</description>
    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.5.1</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.hibernate.orm.tooling</groupId>
                <artifactId>hibernate-enhance-maven-plugin</artifactId>
                <version>${hibernate.version}</version>
                <executions>
                    <execution>
                        <configuration>
                            <failOnError>true</failOnError>
                            <enableLazyInitialization>true</enableLazyInitialization>
                            <enableDirtyTracking>true</enableDirtyTracking>
                            <enableAssociationManagement>true</enableAssociationManagement>
                            <enableExtendedEnhancement>false</enableExtendedEnhancement>
                        </configuration>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <env>
                            <BP_JVM_VERSION>17</BP_JVM_VERSION>
                        </env>
                    </image>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Tronxi
  • 21
  • 4
  • So it seems to generate bytecode at runtime, which of course won't work with native image out of the box, but there is a way to generate them ahead of time -- see [this guide](https://www.graalvm.org/latest/reference-manual/native-image/metadata/#predefined-classes) – peterz Feb 16 '23 at 07:08
  • But using the hibernate plugin should generate code at build time, am I wrong? – Tronxi Feb 16 '23 at 13:32
  • This is a Spring problem. I can't find the issue right now, but the Spring native image build isn't working properly with Hibernate yet. – Christian Beikov Feb 20 '23 at 07:31

0 Answers0