hibernate执行sql 报 No Hibernate Session bound to thread, and configuration does

在SSH框架项目中,当尝试使用`getCurrentSession()`执行SQL查询时,出现错误提示:""No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here""。问题根源在于,对于原生SQL查询,必须在事务中进行。解决方案包括:改用`openSession()`创建Session,或在Hibernate配置中设置`<prop key="hibernate.current_session_context_class">thread</prop>`来启用事务上下文。" 81212528,7242814,新型神经网络:仅凭一张照片实现高精度人脸识别,"['深度学习', '人工智能', '计算机视觉', '面部识别', '神经网络模型']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在SSH框架的项目中,做数据库操作,

Session session = getHibernateTemplate().getSessionFactory().getCurrentSession(); 
但是getCurrentSession()时就会报错:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

报错原因是在dao操作时,使用了原生的SQL语句查询,之前没有报错是因为一直用的是Hql语句,用criteria或者query对象,用HibernateTemplate操作也不报错。解决方法:

如果使用hibernate的getCurrentSession()获得对象,对查询语句也需要开启事务,不然就会报出异常(org.hibernate.HibernateException: createQuery is not valid without active transaction)

 

需将getCurrentSession()改为openSession()或

如果采用jdbc独立引用程序配置如下:<prop key="hibernate.current_session_context_class">thread</prop> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值