【发布时间】:2017-02-26 07:21:30
【问题描述】:
我已将 Spring 从 3.1 迁移到 4.1.3,将 Hibernate 3.2 迁移到 4.3.9
作为迁移的一部分,我在导入和代码下方进行了修改
旧导入
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
新导入
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
和代码更改
在休眠 3 中我有以下代码
public Session getCurrentSession() {
Session session = getSession();
session.setFlushMode(FlushMode.MANUAL);
return session;
}
现在我已经根据新的jars进行了如下修改
public Session getCurrentSession() {
Session session = currentSession();
session.setFlushMode(FlushMode.MANUAL);
return session;
}
经过上述更改后,我遇到了异常
Could not obtain transaction-synchronized Session for current thread
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at org.springframework.orm.hibernate4.support.HibernateDaoSupport.currentSession(HibernateDaoSupport.java:129)
我没有在我的应用程序中使用注释
我无法解决问题。请帮助我了解异常的可能原因
【问题讨论】:
-
修复您的交易设置。这个异常告诉你你没有(正确地)设置你的交易。
-
@M.Deinum 我在配置文件中添加了
标签,但仍然遇到同样的问题。我还需要添加什么吗?请确认 -
只添加该标签而不添加
@Transactional几乎没用。 -
@Raj 如果您没有在项目中使用注释。那么,使用
<tx:annotation-driven/>没有任何好处。 -
@gschambial 是的,但我提供了一个线索,但没有成功:)
标签: spring hibernate spring-4 hibernate-4.x