ssh项目中遇到的懒加载的问题(终于解决啦!!!)

来源:互联网 发布:npc2.wil的算法 编辑:程序博客网 时间:2024/06/04 20:03

转载自:http://hellotommy.iteye.com/blog/809205  


 hibernate3.3.2+spring3.0.3+struts2.2.1

         懒加载(Load On Demand)是一种独特而又强大的数据获取方法,它能够在用户滚动页面的时候自动获取更多的数据,而新得到的数据不会影响原有数据的显示,同时最大程度上减少服务器端的资源耗用。(百度说的)

        通俗点,就是在找一个对象时不找出与他关联的对象,而是在需要相关联对象(或其属性)时才去数据库中找,也称之为延迟加载。

        一般来讲,我们是在many-to-one 的many设置lazy=false,这是hibernate自身提供给我们的。

        后来spring关起来session,我们可以换一种方式:OpenSessionInViewFilter

        OpenSessionInViewFilter是Spring提供的一个针对Hibernate的一个支持类

        一般我们可以这么在web.xml中直接配置

    

Xml代码  收藏代码
  1. <!-- Spring提供的避免Hibernate懒加载异常的过滤器  
  2.   让Session在请求解释完成之后再关闭,从而避免懒加载异常 -->  
  3.   <filter>  
  4.     <filter-name>openSessionInViewFilter</filter-name>  
  5.     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  6.   </filter>  
  7.   
  8. <filter-mapping>  
  9.     <filter-name>openSessionInViewFilter</filter-name>  
  10.     <url-pattern>/*</url-pattern>  
  11.   </filter-mapping>    

 

 

       这样就省去了很多麻烦,就不需要在每一个many地方设置lazy=false了

       这个方法应该很流行,我也是网上找来的。但偏偏我就是新手,直接拿过来用,项目部署了N次都不起效果,真是郁闷。每次报错:org.hibernate.LazyInitializationException: could not initialize proxy - no Session。

       很明显,session关闭了。悲剧

       当时就崩溃,为什么网上都这么说,却总是错误呢。

       知道今天我看到了这篇文章http://www.iteye.com/topic/32001

       突然恍然大雾。

       既然spring管起了sessionFactory,获得session必须也通过他才对呀,所以这部分的mapping 也应该放在struts2的mapping后面,经过这个类,然后执行真正的Action代码,最后根据情况将Hibernate的Session进行关闭。

      下面是完整的web.xml:      

     

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.       
  9.     <!-- struts2 -->  
  10. <filter>  
  11.     <filter-name>struts2</filter-name>  
  12.     <filter-class>  
  13.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  14.     </filter-class>  
  15. </filter>  
  16.     
  17.     
  18.     
  19.     
  20. <!-- spring 整合struts2 -->  
  21. <listener>  
  22.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  23. </listener>  
  24. <context-param>  
  25.     <param-name>contextConfigLocation</param-name>  
  26.     <param-value>classpath:daoContext.xml,classpath:actionContext.xml,classpath:serviceContext.xml,classpath:applicationContext.xml</param-value>  
  27. </context-param>  
  28.   
  29. <!-- Spring提供的避免Hibernate懒加载异常的过滤器  
  30.   让Session在请求解释完成之后再关闭,从而避免懒加载异常 -->  
  31.   <filter>  
  32.     <filter-name>openSessionInViewFilter</filter-name>  
  33.     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  34.   </filter>  
  35.     
  36.     
  37.     
  38.   <!-- 以下2个mapping不可以调换位置 -->  
  39.     
  40.   <filter-mapping>  
  41.     <filter-name>openSessionInViewFilter</filter-name>  
  42.     <url-pattern>/*</url-pattern>  
  43.   </filter-mapping>    
  44.     
  45.     
  46.   <filter-mapping>  
  47.     <filter-name>struts2</filter-name>  
  48.     <url-pattern>*.action</url-pattern>  
  49.   </filter-mapping>  
  50.     
  51.       
  52.   <welcome-file-list>  
  53.     <welcome-file>index.jsp</welcome-file>  
  54.   </welcome-file-list>  
  55. </web-app>  

 

      ok,这样就省去在每个many配置lazy了。

2 0
原创粉丝点击