0

我有一个正在使用 struts2 和 openJPA 构建的项目。我想做一些集成测试,但我似乎在让它工作时遇到了问题。

持久性.xml

<persistence-unit name="SalesCertIT" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>jdbc/salesCertIT</jta-data-source>
    <class>com.ist.salesCert.entities.Certification</class>
    <properties>
        <property name="log4j" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />
        <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
        <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/sales_certification" />
        <property name="openjpa.ConnectionUserName" value="dev" />
        <property name="openjpa.ConnectionPassword" value="password" />
        <property name="openjpa.Id" value="SalesCertIT" />
        <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    </properties>
</persistence-unit>

班级:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("SalesCertIT");
EntityManager em = emf.createEntityManager();

我得到错误:

必须在 ConnectionDriverName 属性中指定 JDBC 驱动程序或 DataSource 类名。

我已将 persistence.xml 和 mysql-connector-java- * -stable-bin-jar 添加到类路径中,(Eclipse->debug Configuration->Classpath->Bootstrap 条目)

如果我尝试在运行时对其进行配置,它确实可以工作,但是在尝试执行操作时会出现另一个错误:

HashMap<String, String> conf = new HashMap<String, String>();
conf.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");
conf.put("openjpa.ConnectionURL", "jdbc:mysql://localhost:3306/sales_certification");
conf.put("openjpa.ConnectionUserName", "dev");
conf.put("openjpa.ConnectionPassword", "password");
conf.put("openjpa.TransactionMode", "local");
conf.put("openjpa.Id", "SalesCertIT");      
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SalesCertIT", conf);
EntityManager em = emf.createEntityManager();

“class com.ist.salesCert.entities.Certification”类型没有得到增强。

我尝试添加一个 javaagent 参数:(Eclipse->Debug Configurations->Arguments->VM arguments)

-javaagent:C:/Progra~1/IBM/WebSphere/AppServer/plugins/com.ibm.ws.jpa.jar

在这一点上,我不知道还能尝试什么。有任何想法吗?

4

1 回答 1

0

一个问题是我试图遵守 JEE6 规范,但 websphere 7 上的 openjpa 2 是 J2EE。我发现增强我的 openjpa 实体的最终解决方案是将 maven 与 openjpa 插件一起使用:

        <!-- openjpa plugin -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <version>1.2</version>
            <configuration>
                <includes>com/ist/salesCert/entities/*.class</includes>
                <excludes>com/ist/salesCert/entitles/Quarters.class</excludes>
                <addDefaultConstructor>true</addDefaultConstructor>
                <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                <persistenceXmlFile>WebRoot/META-INF/persistence.xml</persistenceXmlFile>
                <detail>true</detail>
            </configuration>
            <executions>
                <execution>
                    <id>enhancer</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

然后这个命令将增强实体:

mvn openjpa:enhance

注意:pom.xml 文件中还必须有其他依赖项

于 2012-08-03T22:50:53.457 回答