org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a

来源:互联网 发布:淘宝上质量好的女装店 编辑:程序博客网 时间:2024/05/21 13:22

在用Spring于Hibernate整合的时,在没有配置Spring的事物管理时。使用Hibernate进行事物进行管理时可能会出现此异常

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

原因是你没有配置Spring的事物管理,或者因为hibernate.current_session_context_class的默认值是JTA所以你无法从当前线程中获得一个Session。

解决方法有三种:

1.在Spring中配置Spring的事物管理,我们一般都会使用此种方法。

2.如果你暂时还不想配置Spring的事物管理,此时可以用SessionFactory的OpenSession()方法获得一个新的Session,而不是用getCurrentSession()方法。

3.也可以在配置文件中将hibernate.current_session_context_class的值改为Thread,此时便可以用getCurrentSession()方法从当前线程中获取Session。

 

原创粉丝点击