当前位置: 代码迷 >> Web前端 >> NO.5 hibernate3.3.2 hibernateTemplate load对象页面展示时报错
  详细解决方案

NO.5 hibernate3.3.2 hibernateTemplate load对象页面展示时报错

热度:708   发布时间:2012-08-24 10:00:21.0
NO.5 hibernate3.3.2 hibernateTemplate load对象页面显示时报错
引用:http://blog.163.com/moshen128@126/blog/static/13542109620104135628662/
错误信息:

16:49:00,357 ERROR LazyInitializationException:42 - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.xs.registration.model.User_$$_javassist_0.getUsername(User_$$_javassist_0.java)
16:49:05,975 ERROR LazyInitializationException:42 - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.xs.registration.model.User_$$_javassist_0.toString(User_$$_javassist_0.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

解决:

Entity bean的porxy属性默认是true,会有延迟

在entity

@proxy(lazy=false),会降低性能

解决2:openSessionInView配置成在页面返回给客户端前关闭session

<!--add for lazy load problem  20100514 必须搁在struts2 filter前面,必须的-->
    <filter>
        <filter-name>openSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInView</filter-name>
        <url-pattern>/*</url-pattern> 
    </filter-mapping>
    <!--end for lazy load problem 20100514 --> 
 
    <!--add by xs 20091209  struts 2.1 ng:next generation-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     <!--struts2 源码  -->
    </filter>
  相关解决方案