2012-03-26 74 views
0

我有一个使用struts2和openJPA构建的项目。我想做一些集成测试,但是我似乎遇到了一个让它工作的问题。集成测试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(); 

我得到的错误:

A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.

我已经添加了两个persistence.xml并使用mysql-connector-java的 - * -stable-bin-jar到类路径中,(Eclipse-> debug Configuration-> Class路径 - >引导条目)

如果我尝试在运行时对其进行配置,它的工作,但后来我在尝试进行操作时,另一个错误:

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(); 

The type "class com.ist.salesCert.entities.Certification" has not been enhanced.

我尝试添加javaagent参数:(Eclipse的>设置 - 调试> Arguments-> VM参数)

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

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

+0

你想在JSE或JEE中运行这个吗?在你的第一个配置部分,为什么你有多个openjpa.ConnectionDriverName属性?当你的测试运行时,除xyz之外还有其他有意义的消息还没有被增强吗? – Rick 2012-03-27 14:03:08

回答

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文件中