11

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)

有什么提示吗?

4

4 回答 4

5

利用sessionFactory.getCurrentSession()

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

于 2012-05-02T21:41:42.557 回答
1

您必须添加一个属性示例:

<prop key="hibernate.current_session_context_class">thread</prop>

到您的会话工厂 bean 映射。(查看休眠文档以获取更多详细信息)

于 2012-04-10T14:56:21.087 回答
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.
于 2012-03-19T06:21:48.100 回答
0

org.hibernate.HibernateException: No Session found for current thread当您没有使用 Spring 正确配置事务分界时,会发生“ ”异常。

通常,Spring 在您的事务开始时为您打开一个会话,将其绑定到 ThreadLocal 并在您的事务期间重新使用它。当您的事务提交时,会话将关闭(在刷新之后)。所以,你不需要SessionFactory自己打电话给班级。

通常,人们放置@Transactional一个类(通常是一个服务)并配置 Spring 以启动和结束对该类的方法调用的事务。

于 2012-04-08T12:37:10.357 回答