2013-06-26 77 views
0

我有一個使用Spring 3和Hibernate 3的現有項目。爲了「安全地保護」數據庫一致性,我有以下代碼。如果我打算將項目轉換爲JPA,那麼如何解決transactionInterceptor bean中的transactionManager屬性,因爲JPA使用persistence.xml並且不使用dataSourcesessionFactory我如何使用Spring的TransactionInterceptor和JPA?

<bean id="transactionInterceptor" 
    class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
<property name="transactionManager" ref="transactionManager" /> 
<property name="transactionAttributes"> 
    <props> 
    <prop key="save">PROPAGATION_REQUIRED</prop> 
    </props> 
</property> 
</bean> 

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

回答

0

嘗試使用類似於下面的xml代碼片段的配置。這已經通過Hibernate 4進行了測試,但我希望它也可以與第3版一起使用。

<!-- EntityManagerFactory configuration that doesn't need a persistence.xml --> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
     p:dataSource-ref="dataSource" 
     p:packagesToScan="${jpa.entity.packages}"> 

    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" 
       p:showSql="${hibernate.show_sql}"/> 
    </property> 

    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
     </props> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> 

<!-- Scans for classes/methods with @Transactional annotation to apply the 
    transaction management aspect (TransactionInterceptor) on them. --> 
<tx:annotation-driven/> 
相關問題