我有一个使用Spring 3和Hibernate 3的现有项目。我有以下代码以“安全防范”数据库一致性。如果我要将项目转换为JPA,我如何使用transactionManager
解析transactionInterceptor
bean中的persistence.xml
属性,而不使用dataSource
}和sessionFactory
?
<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 :(得分: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/>