hibernate错误提示: No Hibernate Session bound to thread, and configuration does not allow creation of no

本文介绍了一个HibernateException异常问题及其解决方案。该问题是由于未正确配置事务管理导致的,特别是在方法命名上缺少以“get”开头的方法配置。通过在Spring的事务配置中添加相应的事务属性解决了这个问题。

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

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

    atorg.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)

    atorg.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)

    at com.dao.impl.MyTypeImpl.getParent(MyTypeImpl.java:29)



一直也找不到原因,后来突然发现,自己确实是没有配置事物,自己的那个操作方法名字是:

[java]  view plain  copy
  1. public List<MyType> getParent();  

而事务配置为

[html]  view plain  copy
  1.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  2.     <property name="sessionFactory" ref="sessionFactory"></property>  
  3. </bean>  
  4.   
  5. <tx:annotation-driven transaction-manager="transactionManager" />  
  6.   
  7. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  8.     <tx:attributes>  
  9.         <tx:method name="Add*" propagation="REQUIRED" isolation="DEFAULT" />  
  10.         <tx:method name="Delete*" propagation="REQUIRED" isolation="DEFAULT" />  
  11.         <tx:method name="Update*" propagation="REQUIRED" isolation="DEFAULT" />  
  12.         <tx:method name="Query*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />  
  13.         <tx:method name="LoginInfo*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />  
  14.         <tx:method name="QueryAll*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />          
  15.     </tx:attributes>  
  16. </tx:advice>  

事务里没有配置以“get”开头的方法名,所以导致使用get开发方法错误,加上<tx:method name="get*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />后问题解决,修改后的配置文件如下:


[html]  view plain  copy
  1. <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  2.     <tx:attributes>  
  3.         <tx:method name="Add*" propagation="REQUIRED" isolation="DEFAULT" />  
  4.         <tx:method name="Delete*" propagation="REQUIRED" isolation="DEFAULT" />  
  5.         <tx:method name="Update*" propagation="REQUIRED" isolation="DEFAULT" />  
  6.         <tx:method name="Query*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />  
  7.         <tx:method name="LoginInfo*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />  
  8.         <tx:method name="QueryAll*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />  
  9.         <tx:method name="get*" propagation="NOT_SUPPORTED" isolation="DEFAULT" />  
  10.     </tx:attributes>  
  11. </tx:advice>  

注意方法名称是否在配置的事务方法中


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值