我将 Spring 4.3.4.RELEASE 与 Hibernate 5.1.15
一起使用
作为从3到5的休眠迁移的一部分,我面临与事务注释相关的一些问题。
请考虑一种方法包含两种带有/不带有事务注释的不同方法的情况。
在休眠3中,它曾经可以正常工作。但是在升级到休眠5方法后,setEMSStatus失败了,因为subscribedEms为空。
createAndSaveSubscribedEms不会将数据刷新到数据库。 因此,setEMSStatus不会从数据库获取数据,因此会为null。
显式添加冲洗将起作用。但是,可能有100个地点的情况类似。因此添加显式刷新似乎不是可行的解决方案。
//Class1
@Transactional(rollbackFor = Exception.class)
public void validateAndSave(List<EmsDto> gridList) {
//Removing unnecessary code
emsEntityManager.createAndSaveSubscribedEms(EmsDto.getMessageName(), EmsDto.getDescription(), SubscribeEmsStatus.UNKNOWN.toString());
//Removing unnecessary code
emsEntityManager.setEMSStatus(entry.getKey(), SubscribeEmsStatus.VALID.toString());
}
//Class2
@Transactional
public void createAndSaveSubscribedEms(final String messageName, final String description, final String status) {
session.save(SubscribedEms.create(messageName, description, status));
}
// Note: No transactional and it was working earlier in hibernate 3
public void setEMSStatus(String emsName, String newStatus) {
SubscribedEms subscribedEms = (SubscribedEms) session.createCriteria(SubscribedEms.class).
add(Restrictions.eq(SubscribedEms_.messageName, emsName)).
uniqueResult();
subscribedEms.setStatus(newStatus);
}
我遇到了类似的问题
https://www.javacodegeeks.com/2013/03/migrating-from-hibernate-3-to-4-with-spring-integration.html
但是在休眠5.1.17中,我看不到该类本身。
我应该进行哪些更改才能使其正常工作?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"
>
<context:annotation-config />
<context:component-scan base-package="com.company.product.da">
<context:exclude-filter type="regex" expression="com\.company\.product\..*Test\$.*"/>
<context:exclude-filter type="regex" expression="com\.company\.product\..*TestSupport\$.*"/>
</context:component-scan>
<context:component-scan base-package="com.company.product.entity.platform.event">
<context:exclude-filter type="regex" expression="com\.companthis partct\..*Test\$.*"/>
</context:component-scan>
<jee:jndi-lookup id="jtaTransactionManager" jndi-name="java:/TransactionManager"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="jtaTransactionManager"/>
<import resource="classpath:spring/database-common.xml"/>
<bean id="eventListenerIntegrator" class="com.company.product.persist.hibernate.EventListenerIntegrator"/>
<bean id="hibernateSessionFactory" class="com.company.product.persist.hibernate.CustomHibernateSessionFactoryBean"
depends-on="migrationVersionVerifier">
<constructor-arg ref = "customInfinispanRegionFactory"/>
<constructor-arg ref = "eventListenerIntegrator"/>
<property name="hibernateProperties" ref="optionValueStoreProperties" />
<property name="packagesToScan" value="com.company.product.entity" />
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
我启用了日志以查看使用了哪种事务管理器。
DEBUG [o.s.t.jta.JtaTransactionManager] JTA TransactionManager found at fallback JNDI location [java:/TransactionManager]
INFO [o.s.t.jta.JtaTransactionManager] Using JTA UserTransaction: org.wildfly.transaction.client.LocalUserTransaction@701458cf
INFO [o.s.t.jta.JtaTransactionManager] Using JTA TransactionManager: org.wildfly.transaction.client.ContextTransactionManager@2bba8287
DEBUG [o.s.t.jta.JtaTransactionManager] JTA TransactionSynchronizationRegistry found at default JNDI location [java:comp/TransactionSynchronizationRegistry]
INFO [o.s.t.jta.JtaTransactionManager] Using JTA TransactionSynchronizationRegistry: org.jboss.as.txn.service.internal.tsr.TransactionSynchronizationRegistryWrapper@5437a6d1
session.getTransaction()。getStatus()是 NOT_ACTIVE
session.getFlushMode()是 AUTO
***** jtaTransactionManager有什么问题吗? ****