2

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.

Joachim
  • 51
  • 1
  • 6
  • Have you tried defining a persistence.xml file? Spring generates one dynamically from everything it finds on its class path and its own mechanisms for filtering out what gets into it, but the JPA spec requires a persistence.xml. Hibernate might too. – Chris Nov 07 '22 at 20:00
  • Hello, thank you for your response but i have found the solution (see the answer section). If you know the answer to why it works, feel free to respond :) – Joachim Nov 09 '22 at 08:05

3 Answers3

3

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 :)

Joachim
  • 51
  • 1
  • 6
  • ${hibernate.version} is just a 'hibernate.version' variable defined elsewhere in your files as 5.6.12. This path definition within the plugin configuration is explicitly defining which artifact/version to use, so that is why would pick up and use 5.6.12 instead of the previous dependency's jars. You are giving it a path, so it has to use the files you specify as is. Best practice would be to specify the hibernate.version property in the beginning of your file and use that everywhere a version is required instead of hard coding it in select version tags. For the rest, no clue. – Chris Nov 09 '22 at 16:57
  • Did you switch your entities to use the jakarta packaging for JPA annotations, or are they still using javax? – Chris Nov 09 '22 at 16:58
  • I'm having a similar problem (not solved). One point to note is that the groupId for version 6.1.5.Final is now org.hiberate.orm NOT org.hibernate. The reason you're getting 5.6.12 imported is because of your dependency on spring-boot 2.7.5. – Bruce Stewart Dec 06 '22 at 08:51
0

Updating the version to spring boot 3.0.3-SNAPSHOT works for me.

Parent portion

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.3-SNAPSHOT</version>
    <relativePath/>
</parent>

Model gen related dependencies

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

<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>6.1.7.Final</version>
</dependency>

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>4.0.0</version>
</dependency>
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
0

Try to use hibernate-jpamodelgen-jakarta:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen-jakarta</artifactId>
  <version>5.6.15.Final</version>
</dependency>
ybelin
  • 1