No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here–没有Hibernate会话绑定到线程,并且配置不允许在这里创建非事务性会话。
出现这种异常的原因很多: (springmvc spirng hibernate)框架
一是:使用sessionFactory.getCurrentSession()获取session时出现异常:在加载mvc–servlet(dispatcherservlet.)是开启service注解扫描,此时service层还没有开启事务管理。Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器。而SpringMVC(对应dispatcher_servlet.xml)产生的是子容器。子容器的Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service。为了得到经过事务加强的service实例,在context.xml开启service注解扫描,在servlet.xml排除service扫描
二是:hibernate懒加载造成的:web.xml缺少配置
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三是:没有开启事务配置,或者事务配置属性错误
本文探讨了NoHibernateSessionboundtothread异常的几种常见原因及解决方案,包括调整Spring配置以正确管理事务、添加OpenSessionInViewFilter配置来支持懒加载,以及确保事务配置正确。
1435

被折叠的 条评论
为什么被折叠?



