0

我有一个使用 Spring 3 和 Hibernate 3 的现有项目。我有以下代码来“保护”数据库的一致性。如果我要将项目转换为 JPA,我如何解析beantransactionManager内部的属性,因为 JPA 使用and 不使用and ?transactionInterceptorpersistence.xmldataSourcesessionFactory

<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>
4

1 回答 1

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/>
于 2013-06-26T09:02:58.757 回答