OpenSessionInViewFilter +Redirect in JSF

空扰寡人 提交于 2019-12-11 23:32:45

问题


I have JSF 2.1 application uisng Spring and Hibernate.

In order to solve LazyLoading problem I'm using OpenSessionInView Filter.

<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>

Everything works fine my lazy classes are initiated, but when I'm using Navigation Rule with Redirect

 <navigation-rule>
   <from-view-id>/index.xhtml</from-view-id>
   <navigation-case>
       <from-outcome>fail</from-outcome>
       <to-view-id>/index.xhtml</to-view-id>
       <redirect />
   </navigation-case>
</navigation-rule>

I'm getting org.hibernate.LazyInitializationException: id I remove <redirect /> tag everything works fine.

Why? Is it because new View has been created? So why do I have new Hibernate Session? Is there any work around?


回答1:


<redirect /> is implemented by returning 301 (or 303) response code to the browser, thereby completing current request and closing your current (Hibernate) session.

Browser then submits a new request to the URL provided, resulting in brand new (Hibernate) session which your not-yet-initialized entities are not bound to.

You will either need to manually trigger initialization in existing request prior to redirect (if possible) or re-fetch all the necessary entities again after the redirect.



来源:https://stackoverflow.com/questions/6819539/opensessioninviewfilter-redirect-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!