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

本文介绍了一种在使用Hibernate框架时解决懒加载异常的方法。通过在web.xml中配置Spring提供的OpenSessionInViewFilter过滤器,并调整Struts2和该过滤器的映射顺序,可以有效避免懒加载异常的发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         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中直接配置

    

<!-- Spring提供的避免Hibernate懒加载异常的过滤器
  让Session在请求解释完成之后再关闭,从而避免懒加载异常 -->
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>

<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </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 version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
	<!-- struts2 -->
<filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
</filter>
  
  
  
  
<!-- spring 整合struts2 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:daoContext.xml,classpath:actionContext.xml,classpath:serviceContext.xml,classpath:applicationContext.xml</param-value>
</context-param>

<!-- Spring提供的避免Hibernate懒加载异常的过滤器
  让Session在请求解释完成之后再关闭,从而避免懒加载异常 -->
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  
  
  
  <!-- 以下2个mapping不可以调换位置 -->
  
  <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>  
  
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

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

      看样子对于底层的了解还很不够啊。要加油了!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值