我在application.xml中配了OpenSessionInViewInterceptor怎么还是报could not initialize proxy - the owning Session was closed错误!
我的application.xml中的配置是这样的:
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
</bean>
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
当我用getXXX()就会报could not initialize proxy - the owning Session was closed错误!好像这个配置就没有起到作用~请高手指点下,郁闷了很久了

解决方案 »

  1.   

    把org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor放到web.xml中配置看看,解决懒加载问题似乎要在服务器启动时就装载。
    <!-- 避免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>
      

  2.   

    web.xml方式的配置我也试了,不行~两种方式都应该是可以的呀。
    有高手能指点下用bean的方式让spring支持懒加载~小弟不胜感激~~
      

  3.   

    从来都不用这个来解决 session is close问题。
      

  4.   

    还必须加参数,如下:在web.xml文件中
    -------------------------
    <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
      

  5.   

    <filter>
    <filter-name>openSession</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
    <param-name>flushMode</param-name>
    <param-value>AUTO</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>openSession</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    在web.xml里面配置就好了
      

  6.   

    只在web.xml中配置过,你的那种方式没配置过
      

  7.   

    应该是两个方式都可以呀~一个是在spring容器中过滤,web.xml方式是在spring之前过滤~两个应该是都可以的感觉对两个方式还不是很清楚,有高手能详细说明下两个具体的原理机制~
      

  8.   

    我试了下用下边的这种方式,但是报新的异常~
    <filter>
    <filter-name>openSession</filter-name>
    <filter-class>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
    <param-name>flushMode</param-name>
    <param-value>AUTO</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>openSession</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  9.   

    补充下异常是:
     No WebApplicationContext found: no ContextLoaderListener registered?
      

  10.   

    晕啊,首先这个问题要看你用的是什么mvc框架,如果你用的spring mvc 就用openSessionInViewInterceptor,(称为拦截器模式)
    如果是用的struts1.x之类的mvc框架,就应该用OpenSessionInViewFilter (过滤器模式)
      

  11.   

    struts1.2 和spring2.0
    两个应该是都可以的。
      

  12.   

    唉,还有人这样用。这样已经有发帖讨论过了。用Hibernate.innitlize 方法强制加载是比较好的方法。而不是配什么 openSessionview
      

  13.   

     <!-- OpenSessionInView 配置在struts的mapping前面-->
         <filter-mapping>
             <filter-name>openSessionInViewFilter</filter-name>
             <url-pattern>/*</url-pattern>
         </filter-mapping>
         <!--struts 配置在opensessionInview mapping的后面  -->
         <filter-mapping>
             <filter-name>struts2</filter-name>
             <url-pattern>/*</url-pattern>
       </filter-mapping>注意配置顺序
      

  14.   

    一堆人都没看清楚,别人问的不是web工程,是个应用程序,kao
      

  15.   

    opensession   要有事物。