【发布时间】:2017-02-06 19:53:40
【问题描述】:
@Component
public interface BenefitRateService {
void save(EmployeeBenefits benefitRates) throws Exception;
}
@Service(value="BenefitRateService")
public class BenefitRateServiceImpl implements BenefitRateService {
@Transactional()
public void save(EmployeeBenefits benefitRates) throws Exception{
try{
benefitRateDao.save(benefitRates);
}catch(HibernateException e){
e.printStackTrace();
}
}
}
@Component
public interface BenefitRatesDao {
public void save(EmployeeBenefits benefitRates);
}
@Repository("BenefitRatesDao")
public class BenefitRatesDAOImpl implements BenefitRatesDao {
@Autowired
private SessionFactory springSessionCtxFactory;
public void save(EmployeeBenefits benefitRates) throws Exception{
Session session = springSessionCtxFactory.getCurrentSession();
//session.beginTransaction();
springSessionCtxFactory.getCurrentSession().saveOrUpdate(benefitRates);
//session.getTransaction().commit();
}
}
Spring-config.xml
<tx:annotation-driven transaction-manager="springTransactionManager"/>
<bean id="springTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="springSessionCtxFactory" />
<bean id="springSessionCtxFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.benefits</value>
</list>
</property>
我正在开发一个具有用户条目 EmployeeBenefits 表单的 Web 应用程序。 我正在尝试从数据库中获取 EmployeeBenefits 对象,修改该对象并再次保存。从应用程序的角度来看,似乎数据已保存,当我通过单击“列出所有”按钮检索所有数据时,我可以看到修改后的数据。但问题是数据库中的数据永远不会更新。 保存的对象被缓存在休眠会话的某个地方,但永远不会进入数据库。 请帮忙。
通过将日志级别设置为 Trace,我发现事务实际上正在提交。不知道为什么数据库表不更新。 这是因为 hibernate.connection.autocommit 属性没有设置为 true 吗?
2017-02-06 21:30:15,765 [org.springframework.orm.hibernate3.HibernateTransactionManager:365] DEBUG - Creating new transaction with name [com.vdh.budget.service.impl.BenefitRateServiceImpl.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2017-02-06 21:30:15,786 [org.springframework.orm.hibernate3.HibernateTransactionManager:493] DEBUG - Opened new Session [org.hibernate.impl.SessionImpl@8f2588] for Hibernate transaction
2017-02-06 21:30:15,788 [org.springframework.orm.hibernate3.HibernateTransactionManager:504] DEBUG - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@8f2588]
2017-02-06 21:30:15,796 [org.springframework.transaction.support.TransactionSynchronizationManager:183] DEBUG - Bound value [org.springframework.orm.hibernate3.SessionHolder@66eb46] for key [org.hibernate.impl.SessionFactoryImpl@cdc97b] to thread [main]
2017-02-06 21:30:15,797 [org.springframework.transaction.support.TransactionSynchronizationManager:258] DEBUG - Initializing transaction synchronization
2017-02-06 21:30:15,797 [org.springframework.transaction.interceptor.TransactionInterceptor:362] DEBUG - Getting transaction for [com.vdh.budget.service.impl.BenefitRateServiceImpl.save]
2017-02-06 21:30:15,797 [org.springframework.orm.hibernate3.SessionFactoryUtils:316] DEBUG - Opening Hibernate Session
2017-02-06 21:30:15,806 [org.hibernate.SQL:111] DEBUG - update benefits set amount =? where id =?
2017-02-06 21:30:16,055 [org.springframework.transaction.interceptor.TransactionInterceptor:391] DEBUG - Completing transaction for [com.vdh.budget.service.impl.BenefitRateServiceImpl.save]
2017-02-06 21:30:16,055 [org.springframework.orm.hibernate3.HibernateTransactionManager:925] DEBUG - Triggering beforeCommit synchronization
2017-02-06 21:30:16,055 [org.springframework.orm.hibernate3.SessionFactoryUtils:144] DEBUG - Flushing Hibernate Session on transaction synchronization
2017-02-06 21:30:16,170 [org.springframework.orm.hibernate3.HibernateTransactionManager:752] DEBUG - Initiating transaction commit
2017-02-06 21:30:16,170 [org.springframework.orm.hibernate3.HibernateTransactionManager:652] DEBUG - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@8f2588]
我通过在 @Transactional 注释中添加 (value = "springTransactionManager") 解决了这个问题。 问题是我配置了一个 HibernateTransactionManager,它被设置为从线程中检索会话
<property name="hibernate.current_session_context_class">thread</property>
Spring @Transactional 没有该属性。 所以我配置了一个新的 TransactionalMangaer
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<bean id="springTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="springSessionCtxFactory" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<tx:annotation-driven transaction-manager="springTransactionManager"/>
<bean id="springSessionCtxFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
</props>
</property>
在服务类中
@Transactional(value = "springTransactionManager"})
public void save(EmployeeBenefits benefitRates);
通过拥有两个事务管理器,没有限定符的@Transactional 没有找到我想要的那个。谢谢。
【问题讨论】:
-
尝试刷新会话。或者切换到已经实现所有您需要的 spring-data。
-
@fer.marino 谢谢,但现在无法切换到 spring-data。但是我尝试冲洗但没有用。还有其他建议吗?会话会话 = springSessionCtxFactory.getCurrentSession(); session.saveOrUpdate(benefitRates); session.flush();
-
首先我认为你不需要把@Component放在你的界面上
-
添加
hibernate.cfg.xml的内容。