报错:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
ssh框架(struts2,springmvc,hibernate)
报错语句:Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
原因:使用了原生的sql语句,由于之前一直使用的是hql,但后来因为想用limit对数据库进行操作,但hibernate不支持limit,所以改用了sql语句,以致于程序出错。
简单粗暴的解决方式是 Session session = getHibernateTemplate().getSessionFactory().openSession();
但众所周知 openSession(),getCurrentSession() 有别,区别忘了0.0
还有一种就是对查询语句开启事务管理。
本文探讨了在SSH框架(Struts2,SpringMVC,Hibernate)中使用原生SQL语句导致的HibernateException错误。当尝试使用getCurrentSession()获取会话时,因未在事务中而引发异常。文章提供了两种解决方案:一是使用openSession()直接打开会话,二是对查询语句进行事务管理。
1435

被折叠的 条评论
为什么被折叠?



