I wanted to show the number of items in a certain category and what I get is a LazyInitializationException (failed to lazily initialize a collection of role – no session or session was closed). It seems that because the fetch mode for the collection items is lazy, the session is not available anymore when I actually need it.
The solution is to add an interceptor to the UrlHandlerMapping in the dispatcher servlet like so:
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
<!-- ... -->
</bean>
That interceptor is a OpenSessionInViewInterceptor which will make the session available in the view layer.
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
September 3, 2008 at 10:41 pm |
Your example is not complete. Can you please post a complete example of your bean “urlMapping”.
September 2, 2009 at 5:47 am |
i need sol above problem