2011-01-09 163 views
0

我正在嘗試配置Spring,JPA和DB2,以便在我的彈簧控制器中使用實體管理器實例,但根據我如何配置Spring不會發生這種情況。 這些都是春天的配置的兩次嘗試:Spring DB2 JPA實體管理器問題

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" /> 
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="em" /> 
</bean> 
<bean id="em" 
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="fileUtility" /> 
    <property name="jpaVendorAdapter"> 
    <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"> 
    <property name="database" value="DB2" /> 
    <property name="showSql" value="true" /> 
    </bean> 
    </property> 
</bean> 

二是這樣的:

<!-- Entity manager factory bean. --> 
    <bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="Sample" /> 
    </bean> 

    <!-- Entity manager bean. --> 
    <bean id="em" factory-bean="entityManagerFactory" 
    factory-method="createEntityManager" /> 

和實體管理器以這種方式注入:

<bean id="messageService" class="utilities.services.impl.MessageServiceImpl"> 
    <property name="entityManager" ref="em" /> 
    </bean> 

但我有總是這個例外:

Caused by: java.lang.IllegalArgumentException: methods with same signature createEntityManager() but incompatible return types: [interface com.ibm.websphere.persistence.WsJpaEntityManager, interface org.apache.openjpa.persistence.OpenJPAEntityManagerSPI] 

我不知道如何解決。有沒有人遇到過這個問題?

在此先感謝。

[編輯] 這是我的persistence.xml:

<?xml version="1.0"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> 
    <persistence-unit name="fileUtility" 
     transaction-type="RESOURCE_LOCAL"> 
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 
     <mapping-file>META-INF/mapping.xml</mapping-file> 
     <properties> 
      <property name="openjpa.ConnectionURL" value="jdbc:db2://localhost:50000/db2admin" /> 
      <property name="openjpa.ConnectionDriverName" value="COM.ibm.db2.jdbc.app.DB2Driver" /> 
      <property name="openjpa.ConnectionUserName" value="db2admin" /> 
      <property name="openjpa.ConnectionPassword" value="XXXX" /> 
      <property name="openjpa.FlushBeforeQueries" value="true"/> 
      <property name="openjpa.RuntimeUnenhancedClasses" value="supported" /> 
     </properties> 

    </persistence-unit> 

    <persistence-unit name="fileUtility2" transaction-type="JTA"> 
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 
     <jta-data-source>file_ds</jta-data-source> 
     <mapping-file>META-INF/mapping.xml</mapping-file> 
     <properties> 
      <property name="openjpa.Log" value="SQL=TRACE"/> 
      <property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

回答

1

WebSphere有一個JPA實現捆綁。所以不需要在你的lib中添加openjpa。事實上,WebSphere正在使用OpenJPA,所以你不會失去任何東西。 Look here瞭解更多詳情

當使用jda-data-source時,您需要有transaction-type="JTA"。另外,您不應指定連接屬性 - 它們是在數據源中指定的。

,擺脫了<provider>的 - 我鏈接的文件說:

如果沒有JPA提供商persistence.xml文件中的元素配置的EJB模塊中,默認的JPA供應商,目前配置爲此服務器使用

+0

是的,你是對的,但你知道一些教程,我可以找到一個過程,以獲得配置春天db2和jpa在一起的所有信息?我無法在IBM網站上找到這些信息。 – carlo

+0

但我的問題是如何配置Spring使用JPA和DB2? – carlo

+0

@carlo - 顯示你的persistence.xml – Bozho

1

我相信你做錯了配置,因爲你配置它「àla Tomcat」。如果您使用的是Java EE應用服務器,如WAS,您應該:

  1. 在Spring應用程序上下文XML文件

    • 配置DAO豆由<bean>定義

    • 通過
      <jee:jndi-lookup> 定義來爲在應用服務器中創建的數據源配置JNDI定義; name 屬性應該是 persistence/XXX,其中XXX應與持久性中的 <persistence-unit name="XXX" transaction-type="JTA"> 匹配。xml文件

    • <jee:jndi-lookup id=YYY> id屬性應指向在DAO實體管理器定義的 name=YYY參數,這是說, @PersistenceContext(name=YYY) EntityManager em;

    • 指定 <tx:annotation-driven /><tx:jta-transaction-manager />

  2. 在文件中 web.xml你的網絡應用程序,你應該包括一個使用xml標記的定義 <persistence-unit-ref><persistence-unit-ref-name>參數應該是012xxpersistence/XXX在persistence.xml(如上所示)中指定的JNDI名稱。

  3. 最後,您應該在定義JDBC連接的JNDI名稱的應用程序服務器(AS從屬)中創建一個JNDI定義。此名稱應該與persistence.xml文件中的 <jta-data-source> xml標記匹配,並且它是JPA定義與應用程序服務器中定義的JDBC之間的唯一鏈接。

  4. 圍捕:

    • 應用程序上下文春文件
      <bean class="DAO實現類" />
      <jee:jndi-lookup id="YYY" jndi-name="persistence/XXX" />
      <tx:annotation-driven />
      <tx:jta-transaction-manager />

    • persistence.xml文件
      <persistence-unit name="XXX" transaction-type="JTA">
      <jta-data-source>jdbc/DSN</jta-data-source>
      </persistence-unit>

    • web.x毫升文件
      ...
      <persistence-unit-ref>
      <persistence-unit-ref-name>persistence/XXX</persistence-unit-ref-name>
      </persistence-unit-ref>
      ...
    • DAO(僅@PersistenceContext示出)
      ...
      @PersistenceContext(name = "YYY")
      EntityManager em;
      ...
    • 應用服務器:jdbc/DSN鏈接到連接定義,其中對於DBM駕駛員。取決於使用的AS和DBM。

因此,你可能會看到DAO之間的連接 - > Spring應用上下文文件 - >的persistence.xml和web.xml文件 - >應用程序服務器的JNDI名稱。如果您使用完整的Java EE應用程序服務器(如WAS,Weblogic或GlassFish),則不必使用Spring接口模塊;只有應用服務器中的定義(參見Spring文檔,第12.6.3節)。