Spring整合Hibernate错误:org.hibernate.HibernateException: No Session found for current thread

本文深入探讨了在使用Hibernate框架时遇到的org.hibernate.HibernateException错误,详细解析了四种常见原因,包括注释性事务配置缺失、事务注解遗漏、事务切点配置不当以及事务属性配置错误,并提供了相应的解决方案。

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

一. org.hibernate.HibernateException: No Session found for current thread

原因1:采用 注释性事务时未在applicationContext.xml中开启注释性事务

<tx:annotation-driven transaction-manager="transactionManager"/>

原因2:在注释性事务中,特定方法或类上为加上@Transactional注释

**@Transactional**
public void 方法名(参数列表){...}

原因3:声明式事务中未配置事务切点

<aop:config>
	<aop:pointcut expression="execution(* cn.study.spring.hibernate.service.*.*(..))" id="txPointCut"/>
	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>

原因4:配置事务属性时,未为方法加上事务属性

<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="get*" read-only="true"/>
		**<tx:method name="*" />**
	</tx:attributes>
</tx:advice>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值