JPA2 元模型生成器不会从依赖库生成模型

Mar*_*žik 7 java hibernate jpa eclipselink maven

我的 Maven 项目中有一个依赖库。该库包含 JPA2 实体。在构建过程中仅生成来自根项目中实体的模型,而不生成来自库中实体的模型。我已经尝试过 eclipse 链接生成器(CanonicalModelProcessor)和休眠生成器(JPAMetaModelEntityProcessor)我无法找到任何配置参数,这些参数还包括对元模型生成的项目依赖关系。我也没有找到任何相关的搜索网络。我还尝试通过类标签在persistence.xml中的persistence-unit中手动命名所有实体(包括库实体)

<class>com.example.project.domain.CustomEntity</class>
Run Code Online (Sandbox Code Playgroud)

并尝试通过以下方式打开自动发现

<exclude-unlisted-classes>false</exclude-unlisted-classes>
Run Code Online (Sandbox Code Playgroud)

没有任何帮助。为了列出所有详细信息,我使用的是 Netbeans 8.0.2,项目设置为 Java 7 和 J2EE7。下面是我的两个生成器的 pom 示例。

休眠:

<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>
Run Code Online (Sandbox Code Playgroud)

Eclipse链接:

<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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*žik 2

看来尼尔·斯托克顿是对的。我找到了解决这个问题的两种可能的方法:

  1. 如果依赖库在您的控制之下,那么您可以将 persistence.xml 添加回该依赖项项目,进行构建,让 modelgen 处理器生成模型,然后删除 persistence.xml 以避免在项目中将此文件用作依赖项时可能出现的问题。在 jar 组装过程中使用 Maven 进行简单的自动 persistence.xml 删除如下所示:

<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. 在您的项目和项目依赖项中实现您自己的@Entity注释发现,然后让模型处理器为您生成模型。由于我很幸运地使用了解决方案 1,因此我没有可以实现该解决方案的代码片段。

  • 就我们而言。因为我们拥有依赖项目,所以我们在那里生成代码,在安装/部署之前将编译后的元模型代码添加到 JAR 中。 (2认同)