Spring:为什么Hibernate 4中的SessionFactoryUtils类不提供getSession方法?

gas*_*ldi 8 spring hibernate-4.x

Spring 3.1.0中Hibernate 4的SessionFactoryUtils.getSession方法发生了什么变化?应该用什么呢?sessionFactory.getCurrentSession()不断给我这个异常:

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883)
Run Code Online (Sandbox Code Playgroud)

任何提示?

ian*_*man 5

sessionFactory.getCurrentSession()

我相信“当前会话”的概念出现在 Hibernate 3 中,SessionFactoryUtil是在它存在之前编写的。


cra*_*man 1

不知道他们为什么要删除它,可能他们使用org.hibernate.context.CurrentSessionContext. 从这篇文章来看:

...只需使用普通 SessionFactory 并使用 getCurrentSession 方法即可获取当前事务会话(不要使用 openSession!!!!),然后就可以开始...

根据SessionFactory.getCurrentSession() 的文档

The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use.
Run Code Online (Sandbox Code Playgroud)