org.hibernate.HibernateException: No Session found for current thread
因为项目本身使用的spring3 + hibernate4 + struts2
我在获取当前session时使用的this.sessionFactory.getCurrentSession();出错这应该是在没有打开session(openSession)
解决:
在web.xml下配置如下设置:
<!-- openSessionInView配置 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
注意:一定要将opensessionInView的filter配置在struts2的配置之前否则无效果