Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA2 metamodel generator does not generate models from dependency libraries

I have a dependency library in my Maven project. This library contains JPA2 entities. Only models from entities in root project are generated during the build process, but not models from entities from libraries. I have tried eclipse link generator (CanonicalModelProcessor) and hibernate generator (JPAMetaModelEntityProcessor) I was not able to find any configuration parameters that would include also project dependencies to metamodel generation. I failed to find anything relevant searching web too. I have also tried to manually name all entities (including library entities) in persistence-unit in persistence.xml via class tags

<class>com.example.project.domain.CustomEntity</class>

and also tried to turn on automatic discovery via

<exclude-unlisted-classes>false</exclude-unlisted-classes>

Nothing helped. Just to list all details, I am using Netbeans 8.0.2 and project is set as Java 7 and J2EE7. Bellow are my pom examples for both generators.

Hibernate:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <annotationProcessors>
    <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
        </annotationProcessors>
        <debug>true</debug>
        <optimize>true</optimize>
        <source>1.7</source>
        <target>1.7</target>
        <compilerArguments>
            <AaddGeneratedAnnotation>true</AaddGeneratedAnnotation>
            <Adebug>true</Adebug>
            <endorseddirs>${endorsed.dir}</endorseddirs>
        </compilerArguments>
        <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>4.3.8.Final</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</plugin>

Eclipselink:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
        <execution>
            <id>eclipselink-jpa-metamodel</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <processors>
                    <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                </processors>
                <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.1</version>
        </dependency>
    </dependencies>
</plugin>
like image 838
Marián Mižik Avatar asked Mar 05 '15 10:03

Marián Mižik


1 Answers

Looks like Neil Stockton is right. I have found two possible solutions to this problem:

  1. If the dependency library is under your control, then you can add persistence.xml back to this dependency project, do the build, let modelgen processor generate models and then remove persistence.xml to avoid possible problems in projects where this one is used as dependency. Simple automatic persistence.xml removal using maven during jar assembly would look like this:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <excludes> <exclude>**/persistence.xml</exclude> </excludes> </configuration> </plugin>

  1. Implement your own @Entity annotation discovery in your project and your project dependencies and then let the model processor do the model generation for you. As I was lucky enough to use solution 1, I do not have a code snippet that would implemented this solution.
like image 63
Marián Mižik Avatar answered Oct 16 '22 12:10

Marián Mižik