尽管有OpenSessionInViewFilter,但仍然存在LazyInitializationException

我似乎在Spring / MVC 3.0 / Hibernate 3.5应用程序中随机获取以下LazyInitializationException,尽管看到堆栈跟踪中的filter本身。 关于我应该研究什么的任何想法?

07 Jun 2011 13:48:47,152 [ERROR] (http-3443-2) org.hibernate.LazyInitializationException: could not initialize proxy - no Session org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) at com.test.Image_$$_javassist_18.getMyKey(Image_$$_javassist_18.java) at com.test.AppTagHelper.getAssetUrl(AppTagHelper.java:66) at sun.reflect.GeneratedMethodAccessor81.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstFunction.getValue(AstFunction.java:110) at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935) at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fout_005f2(home_jsp.java:1027) at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fwhen_005f1(home_jsp.java:1002) at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_c_005fchoose_005f1(home_jsp.java:969) at org.apache.jsp.WEB_002dINF.view.home_jsp._jspx_meth_display_005fcolumn_005f0(home_jsp.java:867) at org.apache.jsp.WEB_002dINF.view.home_jsp._jspService(home_jsp.java:214) <> at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198) 

web.xml

  hibernateFilter  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  sessionFactoryBeanName sessionFactory    hibernateFilter *.html   hibernateFilter *.json  

更新,添加SessionFactoryBean定义:

                                        org.hibernate.dialect.Oracle10gDialect ${app.databaseSchema} false false false 60000 REV_TYPE REV_ID    

我知道filter上的延迟加载exception的两个最常见的原因是在exception使Hibernate会话无效之后尝试访问某些内容,或者尝试访问实际上在Web会话中存在的某个字段并且不是附上。

 public interface EntityService { @Transactional EntityA getA(Long id); @Transactional EntityB getB(Long id); } public class WebPageController { public void handleGet(Long id1, Long id2) { EntityA a = entityService.getA(id1); try { EntityB b = entityService.getB(id2); } catch (Exception e) { //print somthething } a.accessLazyField(); //will throw lazy load after getB throws exception } } 

很明显,为了清楚起见,省略了大量的代码和注释:)

 @SessionAttributes("model") public class WebPageController { @ModelAttribute("model") @RequestMapping(method=RequestMethod.GET) public EntityA handleGet(Long id) { return entityService.getA(id); } @RequestMapping(method=RequestMethod.POST) public String handlePost(@ModelAttribute("model") EntityA a) { a.accessLazyField(); //will throw lazy load if the field was not accessed during original page rendering return "viewName"; } } 

在Spring MVC你应该使用OpenSessionInViewInterceptor而不是filter在我当前的项目中,这是这样配置的:

         

sessionFactory引用org.springframework.orm.hibernate3.LocalSessionFactoryBean

工作完美无瑕。