这种异常有时是因为在用Hibernate的load方法取数据库的数据时发生的,因此往往load出来的对象是一个Proxy即一个代理对象,而取出来的方法执行完毕后,session就会关闭,而当返回这个代理对象到客户端解析jsp页面时才会真正的发SQL语句,而此时的session已经关闭,因此会抛出此异常。解决方法如下:
(1)使用get方法来替代load方法,但是要注意的时若get方法的数据表若还关联了其他的对象则依然不会在get对象时发出SQL语句,依然会出现这个异常。
(2)扩大session的生命周期。即可采用Spring提供的一个filter:OpenSessionInView 。
完整的filter配置如下:
<filter>
<filter-name>O
penSessionInView
</filter-name>
<filter-class>org.springframework
.orm.hibernate3
.supp
ort.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView
</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:这个filter必须写在web.xml文件的filter名为struts2的前面。
(1)使用get方法来替代load方法,但是要注意的时若get方法的数据表若还关联了其他的对象则依然不会在get对象时发出SQL语句,依然会出现这个异常。
(2)扩大session的生命周期。即可采用Spring提供的一个filter:OpenSessionInView 。
<filter>
</filter>
<filter-mapping>
</filter-mapping>