spring+Hibernate5的Could not obtain transaction-synchronized Session for current thread解决

2018-2-5

结合https://www.cnblogs.com/biobio/p/7754080.html

 http://blog.csdn.net/frankcheng5143/article/details/51308344

成功解决问题。

前言

在整合springmvc4+spring4+hibernate5时,DAO层在getCurrentSession()时报错:Request processing failed; nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

错误跟踪到sessionFactory.getCurrentSession();

protected Session getCurrentSession() {

  return this.sessionFactory.getCurrentSession(); }



解决方案

原理都在上面两个链接,我也是看的一脸懵逼,不过我直接把方法给出,只需要修改两个文件,也就是applicationContext.xml和springmvc.xml,下面就是我的成功配置。

-------------------------------------------------------------------------------

可能有些人的@Transactional配置是在dao层,所以要要要把springmvc.xml文件中org.springframework.stereotype.Service和org.springframework.stereotype.Repository都都都不扫描。


-------------------------------------------------------------------------------
applicationContext.xml(只扫描dao层和Service层)

  <!-- 扫描路径,不扫描Controller -->
    <context:component-scan base-package="casic.bj(改为你的包名)">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

-------------------------------------------------------------------------------
springmvc.xml中(只扫描@controller)

    <context:component-scan base-package="casic.bj(改为你的包名)">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation"
             expression="org.springframework.stereotype.Service" />
     <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Repository" />
</context:component-scan>


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值