JPA实体在从另一个JAR引用时显示为未增强

时间:2016-02-10 10:26:37

标签: java maven jpa

我有一个Maven Java项目,其中包含我的业务逻辑和JPA持久性实体的JAR。这些实体在构建时由Maven插件增强。我还有另一个项目依赖于该JAR。即。

EAR
|->JAR - with entities and persistence.xml
|->WAR - dependent on JAR above

当我从JAR中访问实体时,一切正常。但是,如果我尝试从WAR文件中访问实体,我会收到此错误:

11:55:07,916 ERROR [org.jboss.as.ejb3.invocation] (default task-4) JBAS014134: 
EJB Invocation failed on component PersonResource for method 
public za.co.shared.PersonDTO za.co.ws.PersonResource.get(java.lang.Long):

javax.ejb.EJBException: <openjpa-2.3.0-r422266:1540826 nonfatal user error> 
org.apache.openjpa.persistence.ArgumentException: This configuration disallows 
runtime optimization, but the following listed types were not enhanced at 
build time or at class load time with a javaagent: 

... long list of all my entities

 The type "class za.co.entities.Person" has not been enhanced.

这对我没有意义,因为这些类在构建时肯定会得到增强。

JAR项目的我的POM包含:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
        <includes>za.co.entities.Person</includes>
        <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
    </configuration>
    <executions>
        <execution>
            <id>JPA Enhance</id>
            <phase>process-classes</phase>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>
</plugin>

可以从不同的库中引用JPA实体吗?

修改 我已经反编译了WAR文件和其中引用的JAR,我可以看到这些类实际上已经增强了。很明显,它的工作方式不是问题。可能是某种配置问题?

1 个答案:

答案 0 :(得分:0)

我将所有JPA实体移动到他们自己的JAR文件中,并从我的EJB模块和WAR中引用它。即。

EAR
|->JAR - JPA entities only and pesistence.xml
|->JAR - business logic in EJBs (including EntityManager calls to retrieve data)
|        dependent on JAR above.
|->WAR - dependent on the first JAR only

通过这种方式,WAR文件中的代码可以访问第一个JAR中的JPA实体。但是,如果我将第二个JAR添加为WAR文件中的依赖项并尝试访问实体管理器调用,则会返回原始错误。此外,在WAR中禁用了JPA实体上的延迟加载。