欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

could not initialize proxy - the owning Session wa HibernateJavaApacheSpringSUN

程序员文章站 2022-03-07 15:13:18
...

org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
 at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
 at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
 at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
 at com.zhjy.gea.module.affair.domain.Affair$$EnhancerByCGLIB$$e10be950.getAffairCatalogs(<generated>)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:585)
 at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2049)
 at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1286)
 at org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:284)
 at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:137)

分析原因:
由于Spring控制的Hibernate的生命周期只针对数据层和服务层,而未管理到表现层,所以会出现the owning Session was closed的现象。针对这一点,我通过hibernate filter的方式来解决。

解决方法:
在web.xml中配置filter来解决。

<!--  hibernate session filter  --> 
  < filter > 
   < filter-name > hibernateFilter </ filter-name > 


   < filter-class >    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
   </ filter-class > 
  </ filter > 
 


 < filter-mapping > 
   < filter-name > hibernateFilter </ filter-name > 
   < url-pattern > *.do </ url-pattern > 
  </ filter-mapping >