Maven Open JPA插件增强:似乎工作但它没有增强我的实体

时间:2012-11-29 01:47:22

标签: struts2 maven-3 netbeans-7 openjpa openjpa-maven-plugin

当我编译我的Maven项目时,它说我的实体已经增强。但是,当我通过EntityManagerFactory启动数据库连接时,错误发生在这里的代码:em = factory.createEntityManager();我相信我已经遵循网络中的所有步骤..但是我遇到了这个错误。请帮忙......对此有何建议?非常感谢你。

错误导致错误的部分代码。

factory = Persistence.createEntityManagerFactory("LotMovementPU");
        em = factory.createEntityManager();

Tihs是编译时的日志。

nothing to compile - all classes are up to date

[openjpa:enhance]
52  LotMovementPU  INFO   [main] openjpa.Tool - Enhancer running on type "class lotmovement.business.entity.UserProfile".

[resources:testResources]
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\god-gavedmework\Documents\NetBeansProjects\lotmovementMaven\src\test\resources

[compiler:testCompile]
No sources to compile

[surefire:test]
No tests to run.
Surefire report directory: C:\Users\god-gavedmework\Documents\NetBeansProjects\lotmovementMaven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0


[war:war]
Packaging webapp

Warning: selected war files include a WEB-INF/web.xml which will be ignored 
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 8.386s
Finished at: Thu Nov 29 14:21:58 NZDT 2012
Final Memory: 22M/437M
------------------------------------------------------------------------
NetBeans: Deploying on Apache Tomcat 7.0.27.0
    profile mode: false
    debug mode: true
    force redeploy: true

步骤到em.factory.createEntityManager();

时出错
<openjpa-2.2.0-r422266:1244990 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: "
lotmovement.business.entity.UserProfile".

的pom.xml

<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>com.mycompany</groupId>
    <artifactId>LotMovement</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>LotMovement</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <netbeans.hint.deploy.server>Tomcat</netbeans.hint.deploy.server>
    </properties>


    <repositories>
        <repository>
            <id>JBoss Repo</id>
            <url>http://repository.jboss.com/maven2</url>
            <name>JBoss Repo</name>
        </repository>
        <repository>
            <id>ibiblio mirror</id>
            <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
        </repository>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
        <repository>
            <url>http://ftp.ing.umu.se/mirror/eclipse/rt/eclipselink/maven.repo</url>
            <id>eclipselink</id>
            <layout>default</layout>
            <name>Repository for library Library[eclipselink]</name>
        </repository>
    </repositories>



    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <!-- set the version to be the same as the level in your runtime -->
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-all</artifactId>
            <version>2.2.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <!-- set the version to be the same as the level in your runtime -->
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>


        <!-- Spring framework --> 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6.SEC03</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>2.5.6.SEC03</version>
        </dependency>

        <!-- Struts 2 + Spring plugins -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.7</version>
        </dependency>


        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derbyclient</artifactId>
            <version>10.9.1.0</version>
        </dependency>

        <dependency>

            <groupId>org.apache.derby</groupId>

            <artifactId>derby</artifactId>

            <version>10.9.1.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.derby</groupId>

            <artifactId>derbynet</artifactId>

            <version>10.9.1.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.derby</groupId>

            <artifactId>derbytools</artifactId>

            <version>10.9.1.0</version>

        </dependency>







    </dependencies>



    <build>


        <resources>
            <resource>
                <directory> src/main/java </directory>
                <includes>
                    <include> **/*.xml </include>
                </includes>
            </resource>



        </resources>



        <plugins>





            <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <includes>lotmovement/business/entity/*.class</includes>
                    <addDefaultConstructor>true</addDefaultConstructor>
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>

                    <!-- Pass additional properties to the Plugin here -->
                    <toolProperties>
                        <property>
                            <name>directory</name>
                            <value>otherdirectoryvalue</value>
                        </property>
                    </toolProperties>

                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>



            </plugin>






            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="LotMovementPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <jta-data-source>openjpa</jta-data-source>
    <class>lotmovement.business.entity.UserProfile</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
      <property name="javax.persistence.jdbc.password" value="app"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="app"/>
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
    </properties>
  </persistence-unit>
</persistence>

关于构建这个的ANT风格,这是在BUILD.XML中找到的也许我的增强器没有发布编译增强它?如果是的话,你怎么能在maven中进行后编译增强?

build.xml以增强ant中的实体。

<target name="-post-compile">
        <!-- Empty placeholder for easier customization. -->
        <!-- You can override this target in the ../build.xml file. -->

          <echo message="begin openJPAC"/>
        <path id="openjpa.path.id">        
            <pathelement location="${build.classes.dir}"/>

             <!-- Adding the OpenJPA jars into the classpath -->
            <fileset dir="D:\openjpa\apache-openjpa-2.2.0\" includes="*.jar"/>
             <!--  or if you create a OpenJPA Library you can use that instead  -->
            <!--<pathelement path="${libs.OpenJPA.classpath}"/>-->
        </path>

        <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
            <classpath refid="openjpa.path.id"/>
        </taskdef>


        <openjpac>            
            <classpath refid="openjpa.path.id"/>
        </openjpac>
        <echo message="end openJPAC"/>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我删除了插件“openjpa-maven-plugin”并将其替换为POM.XML - Plugin元素中的(请参阅下面的插件。)。编译时,您将在下面看到此消息。请参阅编译消息。顺便说一句,我使用的是Netbeans 7.2.1,Maven和struts2-spring插件。

编译消息。     [antrun:运行]     执行任务          [java] 63 LotMovementPU INFO [main] openjpa.Tool - 在“lotmovement.business.entity.UserProfile”类型上运行的Enhancer。     执行的任务

的pom.xml

<plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>process-classes</phase>
            <configuration>
              <tasks>
                <java classname="org.apache.openjpa.enhance.PCEnhancer"
                      classpathref="maven.runtime.classpath"
                      dir="target/classes" fork="true" />
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>