1

当我编译我的 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>

持久性.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中找到的可能我的enhancer没有post compile enhance吧?如果是,您如何在 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"/>
4

1 回答 1

0

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

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

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>
于 2012-11-29T19:12:07.543 回答