Spring控制的Hibernate的生命周期只针对数据层和服务层,表现层就没这个福分了还有就是只有在session没有关闭的情况下运行Obj.getXxx()时才会执行select语句从数据库加载对象(这就是所谓的延迟加载)session已经关闭,此时再运行Obj.getXxx()方法,Hibernate就会抛出"Could not initialize proxy - the owning Session was closeed"的异常。不过可以通过 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter过滤器来弥补这一缺陷。在WEB.XML中加入以下内容:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
==============如果还是不行则在DAO里加入Hibernate.initialize(Obj.getXxx());
posted on 2008-04-10 23:44
...... 阅读(389)
评论(0) 编辑 收藏