spring 管理 hibernate 事物,抛异常 org.hibernate.HibernateException: save is not valid without active transa

在使用spring 管理hibernate事物时

spring 中配置:
        <!-- 配置申明式的事务管理(基于注解)事务加在父类上 子类也有事务 所以事务一般加载 baseservice 上 -->
        < bean  id =  "transactionManager"  class = "org.springframework.orm.hibernate4.HibernateTransactionManager"  >
              < property  name = "sessionFactory"  ref = "sessionFactory"  ></ property >
        </ bean >
        < tx:annotation-driven  transaction-manager = "transactionManager"  />

但是在测试时 抛出异常:
org.hibernate.HibernateException: save is not valid without active transaction
该异常表示 hibernate 没有拿到事务。

查看hibernate.cfg.xml 中配置:
<!-- Enable Hibernate's automatic session context management -->
  < property  name = "current_session_context_class" > thread </ property >
current_session_context_class    表示当前 在调用getcurrentSession 方法是 是冲那里获取 事务
 表示重线程中获取事务。

百度后 了解 此处配置有问题应该为:
<  property  name  = "current_session_context_class" > org.springframework.orm.hibernate4.SpringSessionContext  </ property >
表示 spring 为 当前session 提供事务。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值