Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate the JPA metamodel files using maven-processor-plugin - What is a convenient way for re-generation?

I am trying to use maven-processor-plugin for generating JPA metamodel java files and I set up my pom.xml as belows.

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <compilerArgument>-proc:none</compilerArgument>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <executions>
            <execution>
                <id>process</id>
                <goals>
                    <goal>process</goal>
                </goals>
                <phase>generate-sources</phase>
                <configuration>
                    <!-- source output directory -->
                    <outputDirectory>${basedir}/src/main/java</outputDirectory>
                    <processors>
                        <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                    </processors>
                    <overwrite>true</overwrite>
                </configuration>
            </execution>
        </executions>
    </plugin>

Actually, I want to generate metamodel files (Entity_.java) to the same packages of their corresponding entities (Entity.java). Hence, I set up outputDirectory in the plugin as

<outputDirectory>${basedir}/src/main/java</outputDirectory>

The first time of running is ok, but from later times when performing the metamodel java files re-generation, the plugin always turns out an error about file duplication.

My Question is - Is there any way to config the plugin so that it could overwrite the existing files during re-generation?

In fact, to work around

  1. I must delete all generated files before any re-generation.
  2. I could point the outputDirectory to a different folder in /target, this location will be clean everytime Maven run, but this leads to manually copy generated metamodel files to source folder for update after re-generation.

Both of these are very inconvenient and I hope you guys could show me a proper solution.

like image 662
Chiến Nghê Avatar asked Jun 10 '15 18:06

Chiến Nghê


People also ask

How do I generate a JPA metamodel class?

Generating JPA Metamodel Classes Note, we need to add the target/generated-classes folder to the classpath of our IDE, as by default, the classes will be generated in this folder only.

What is metamodel in JPA?

JPA (Java persistence API) metamodel classes are classes that describe the structure of JPA entity classes (classes used to store database state as Java objects). They enable you to write Java code for creating database queries in a typesafe way.

What is metamodel in Java?

The Metamodel API is used to create a metamodel of the managed entities in a particular persistence unit. For each entity class in a particular package, a metamodel class is created with a trailing underscore and with attributes that correspond to the persistent fields or properties of the entity class.

What is Maven processor plugin?

A maven plugin to process annotation for jdk6 at compile time This plugin helps to use from maven the new annotation processing provided by JDK6 integrated in java compiler This plugin could be considered the 'alter ego' of maven apt plugin http://mojo.codehaus.org/apt-maven-plugin/


1 Answers

In case you your have another annotation processor used during compilation (like lombok) you might run into following error (depite having hibernate-jpamodelgen added as dependency):

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /your/not/CompilableClass:[5,68] cannot find symbol
  symbol:   class YouEntity_
  location: package your.not
[INFO] 1 error

In this case you need to declare both annotation processors directly for your compiler plugin (for both lombok and hibernate):

            <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.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-jpamodelgen</artifactId>
                            <version>${hibernate-jpamodelgen.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
like image 147
walkeros Avatar answered Oct 17 '22 04:10

walkeros