延时加载问题解决OpenSessionInViewInterceptor 和 OpenSessionInViewFilter的配置

在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception;   

Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题.    
    它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具体参看 SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。    
     Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。

OpenSessionInViewInterceptor配置:

<beans>     
<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">     
<property name="sessionFactory">     
<ref bean="sessionFactory"/>     
</property>     
</bean>     
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">     
<property name="interceptors">     
<list>     
<ref bean="openSessionInViewInterceptor"/>     
</list>     
</property>     
<property name="mappings">     
....     
</property>     
</bean> ...
</beans> 

OpenSessionInViewFilter配置:

<web-app>     
  <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>
 ...
 </web-app>   


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值