Configure hibernate-jpamodelgen in Maven
Asked Answered
S

6

5

I want to configure hibernate-jpamodelgen into Maven pom.xml. I tried this:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>plugin</groupId>
    <artifactId>org.plugin</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>Plugin</name>
    <url>http://maven.apache.org</url>

    <parent>
         ........
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.4.3.Final</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>datalis_plugin</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.6</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArguments>
                        <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                    </compilerArguments>
                </configuration>
            </plugin>               
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>   
</project>

Full POM: https://pastebin.com/VjucMAYL

But I get error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project org.plugin: Compilation failure
[ERROR] Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found

Do you know how I can fix this issue?

I used this quite: https://docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/

Sharl answered 31/7, 2019 at 12:36 Comment(2)
Which version of Java are you using ?Bora
Latest - java version "12.0.1" 2019-04-16Sharl
M
7

I usually just add the hibernate-jpamodelgen to the annotationprocessorpath of the compiler plugin. That prevents the processor to be packaged in the deployment.

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <annotationProcessorPaths>
        <path>
          <groupId>org.mapstruct</groupId>
          <artifactId>mapstruct-processor</artifactId>
          <version>${org.mapstruct.version}</version>
        </path>
        <path>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-jpamodelgen</artifactId>
          <version>5.4.3.Final</version>
        </path>
      </annotationProcessorPaths>
    
    </configuration>
  </plugin>
Mother answered 12/5, 2021 at 8:55 Comment(1)
If you are using Spring Boot, you can use <version>${hibernate.version}</version> so that the version is aligned with the one that Spring Boot recommends.Lund
W
3

Remove <scope>provided</scope> from

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

Add plugin

    <plugins>
        ...
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                        </processors>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>
                    <version>5.4.3.Final</version>
                </dependency>
            </dependencies>
        </plugin>
        ...
    </plugins>

And then rebuild

mvn clean package -DskipTests
Weatherman answered 1/8, 2019 at 7:40 Comment(4)
Thanks, yesterday I tried this but I get Plugin execution not covered by lifecycle configuration: org.bsc.maven:maven-processor-plugin:3.1.0:process (execution: process, phase: generate-sources) at line 218 here: pastebin.com/kRLDwa8sSharl
Let me try to solve it by adding <pluginManagement>Sharl
Unfortunately I can build the project but again meta models are not generated. ... Very strange.....Sharl
Do I need to make some specific configuration for Spring?Sharl
P
2

This works for me

pom.xml:

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jpamodelgen</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                        <version>2.2.11.RELEASE</version>
                    </path>
                    <path>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                            <version>5.4.22.Final</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
  </plugins>
</build>

<profiles>
  <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
            </dependency>
        </dependencies>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
</profiles>
import com.mycompany.myapp.domain.*; // for static metamodels
import com.mycompany.myapp.domain.User

public class UserQueryService {

private Specification<User> createSpecification(UserCriteria criteria) {
  Specification<User> specification = Specification.where(null);
  if (criteria != null) {
    if (criteria.getId() != null) {
       specification = specification.and(buildSpecification(criteria.getId(), User_.id));
    }
  }
  return specification;
}    
}
Plasterwork answered 24/8, 2021 at 13:38 Comment(0)
B
1

In case of Java 12, use the below code snippet inside build in pom.xml.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>12</release>  
    </configuration>
</plugin>

After this, add the below for jpamodelgen.

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>5.4.3.Final</version>
  <optional>true</optional>
</dependency>
Bora answered 31/7, 2019 at 13:8 Comment(9)
What about <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>?Sharl
Looks like it's not working. I'm trying to implement this code: pastebin.com/GW7MxMDN I get cannot find symbol [ERROR] symbol: variable PaymentTransactions_ probably because PaymentTransactions_ is not generated. Do you know should I create PaymentTransactions_ manually?Sharl
Let me check, by the way, are you able to build the project using maven command ?Bora
using clean package?Sharl
It means you are able to build the project using maven command, it is about not generating the classes. Can you check this link. #30764766Bora
You can also check this link. github.com/jinahya/jpa-metamodels-with-maven-example/blob/…Bora
But I think it's for older Java versions?Sharl
I can confirm that the answer of @Bora works. His configuration makes the files being generated at target/generated-sources/annotations. This can easily be overseen. The directory can be added as source folder in IDEs as eclipse. When doing so compile errors for non resolvable types in IDEs may be solved.Punchdrunk
BTW: No other plugins or configurations are necessary for jpa static model generation!Punchdrunk
F
1

Adding hibernate-jpamodelgen to my dependencies did not do the job for me, but rather created dependency conflicts, which caused the dependencyConvergence of the maven-enforcer-plugin to complain unless I combined it with scope 'protected'. Besides, it does not seem plausible to me to declare a runtime dependency unless I have one.

It turned out not to be necessary for me: I used Cesar's approach, but unlike in Cesar's case, I did not need to add hibernate-jpamodelgen or anything else to my dependencies.

I already had a processor added to the maven-compiler-plugin, namely mapstruct-processor. Like Cesar suggested, I added the hibernate-jpamodelgen to that configuration. In my case, the result was:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>11</source>
        <target>11</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.2.Final</version>
            </path>
            <!-- The following path declaration is the only thing I added to make hibernate-jpamodelgen run -->
            <path>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-jpamodelgen</artifactId>
                <version>5.3.20.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>
Fic answered 19/5, 2023 at 6:48 Comment(0)
L
0

Proper solution with "maven-compiler-plugin".

If you use version higher than 3.1, use tag compilerArgs.

If you use version lower than 3.1, use tag compilerArguments

Do not forget to delete the appropriate tag not appropriate to your version.

Why "build-helper-maven-plugin" is used as well, check answer from Martin Baumgartner in this thread.

By putting your generated sources into target, you would face the problem within your IDE because the related code can't be found. Therefore you can add the build-helper-maven-plugin to dynamically add the folder from the target directory

 <build>
    <directory>${project.basedir}/target</directory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.plugin.compiler.version}</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.hibernate.orm</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs> <!-- Use this for plugin version HIGHER than 3.1, delete the other one -->
                    <arg>-processor</arg>
                    <arg>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</arg>
                </compilerArgs>
                <compilerArguments> <!-- Use this for plugin version LOWER than 3.1, delete the other one -->
                    <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${build-helper-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${project.basedir}/target/generated-sources/annotations</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
Longspur answered 14/5 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.