OpenSessionInViewFilter +Redirect in JSF
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?
<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.
精彩评论