您的位置:首页 > 编程语言 > Java开发

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

2018-02-05 23:59 766 查看
2018-2-5结合https://www.cnblogs.com/biobio/p/7754080.html http://blog.csdn.net/frankcheng5143/article/details/51308344成功解决问题。前言在整合springmvc4+spring4+hibernate5时,DAO层在getCurrentSession()时报错:Requestprocessing failed; nested exception is org.hibernate.HibernateException: Couldnot 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
a1f2
-->
<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>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐