2012-03-26 33 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文件中