Search code examples
javaspring-boothibernatejpajpamodelgen

Static jpa metamodel are not generated with hibernate-jpamodelgen 6.1.5.Final


I know this question has been asked several times but none of the answers worked for me.

Basically, i'm trying to generate the jpa metamodel from my entities in order to use them in specifications. However, despite running (i think, see the rebuild project ouput screenshot), the Hibernate JPA 2 Static-Metamodel Generator 6.1.5.Final didn't generate any classes in the annotations folder.

Can someone please help me? (PS sorry i cannot post screenshot because stack overflow doesn't let me)

I'm using java 19 with sping boot 2.7.5 and maven and intellij.

Here is the Test.java file (the class i want to generate):

package com.example.demo.model;


import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Test {
    @Id
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

The pom.xml:

<?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>2.7.5</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>
   <properties>
      <java.version>19</java.version>
   </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-test</artifactId>
         <scope>test</scope>
      </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
      <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-jpamodelgen</artifactId>
         <version>6.1.5.Final</version>
      </dependency>
      <dependency>
         <groupId>jakarta.xml.bind</groupId>
         <artifactId>jakarta.xml.bind-api</artifactId>
         <version>4.0.0</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

The rebuild project output:

Clearing build system data...
Executing pre-compile tasks...
Cleaning output directories…
Running 'before' tasks
Checking sources
Copying resources... [demo]
Parsing java… [demo]
java: Hibernate JPA 2 Static-Metamodel Generator 6.1.5.Final
Writing classes… [demo]
Updating dependency information… [demo]
Adding @NotNull assertions… [demo]
Adding pattern assertions… [demo]
Adding Threading Model assertions… [demo]
Parsing java… [tests of demo]
Writing classes… [tests of demo]
Updating dependency information… [tests of demo]
Adding @NotNull assertions… [tests of demo]
Adding pattern assertions… [tests of demo]
Adding Threading Model assertions… [tests of demo]
Running 'after' tasks
javac 19 was used to compile java sources
Finished, saving caches…
Executing post-compile tasks...
Finished, saving caches…
Synchronizing output directories...
06-11-22 14:37 - Build completed successfully in 3 sec, 464 ms

And the intellij annotation processors settings:

I cannot post images but the enable annotation processing is checked as well as obtain processorts from projet classpath and the path is target\generated-sources\annotations in the module content root.

After rebuilding the project, the target folder appears and contains a folder named generated-sources. This generated-sources contains a folder called annotations but it doesn't contain anything (but it should contain the class Test_).

The folder annotations has been set as a source folder aswell.

I have read some previous posts but none were working for me. I also read the documentation (doc) but it still doesnt work.


Solution

  • I have found two solutions but i don't understand why it works (i'm lucky i guess). Any explanations would be great :)

    1. First solution

    Use the version 5.6.12.Final of Hibernate directly in a dependency. If i upgrade it to version 6.1.5.Final (as suggested by intellij), the metamodel are not generated but i don't know why...

    Dependency

    1. Second solution

    Set the dependency to the latest version (here 6.1.5.Final) but add a plugin to the build with an annotationProcessorPaths that use the version 5.6.12.Final

    annotationProcessorPaths

    PS: i don't understand why the variable ${hibernate.version} is set to version 5.6.12.Final while i have set my hibernate verison to 6.1.5.Final in the dependency... Any explanation would be great :)