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

在用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。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值