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/

Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

5 Answers5

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>
Marcus Schulz
  • 123
  • 1
  • 6
  • 1
    If you are using Spring Boot, you can use `${hibernate.version}` so that the version is aligned with the one that Spring Boot recommends. – Wim Deblauwe Dec 15 '21 at 09:09
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
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
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;
}    
}
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>
Sambit
  • 7,625
  • 7
  • 34
  • 65
  • What about org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor? – Peter Penzov Jul 31 '19 at 14:05
  • Looks like it's not working. I'm trying to implement this code: https://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? – Peter Penzov Jul 31 '19 at 14:14
  • Let me check, by the way, are you able to build the project using maven command ? – Sambit Jul 31 '19 at 14:15
  • using `clean package`? – Peter Penzov Jul 31 '19 at 14:24
  • It means you are able to build the project using maven command, it is about not generating the classes. Can you check this link. https://stackoverflow.com/questions/30764266/generate-the-jpa-metamodel-files-using-maven-processor-plugin-what-is-a-conven – Sambit Jul 31 '19 at 14:31
  • You can also check this link. https://github.com/jinahya/jpa-metamodels-with-maven-example/blob/master/pom.xml – Sambit Jul 31 '19 at 14:32
  • But I think it's for older Java versions? – Peter Penzov Jul 31 '19 at 14:57
  • I can confirm that the answer of @Sambit 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. – r-uu Sep 28 '22 at 08:13
  • BTW: No other plugins or configurations are necessary for jpa static model generation! – r-uu Sep 28 '22 at 08:19
0

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>