集成测试OpenJPA数据访问对象或服务外观?

时间:2012-03-26 16:47:30

标签: java integration-testing openjpa persistence.xml

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

的persistence.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();

我收到错误:

  

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

我已将persistence.xml和mysql-connector-java- * -stable-bin-jar添加到类路径中,(Eclipse-&gt; debug Configuration-&gt; Classpath-&gt ; 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-&gt; Debug Configurations-&gt; Arguments-&gt; VM参数)

  

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

此时我不知道还有什么可以尝试。有什么想法吗?

1 个答案:

答案 0 :(得分: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文件中还有其他依赖项