views:

123

answers:

1

I'm building an application in Hibernate, Spring and JSF2 using only annotations. How can I take advantage of OpenSessionInViewInterceptor found in Spring to catch any hibernate session that might open within a bean?

I'm trying to elegantly solve the common “failed to lazily initialize a collection of role: your.Class.assocation no session or session was closed.” problem when trying to read from a yet uninitialized list of POJOs inside another POJO (A Tag entity retrieved by a DAO that contains a List of Project objects I want to read). I've found this:
http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/
but failed to make use of it in my environment.

Please provide a detailed answer, as the Internet is full of foggy, unhelpful tutorials. I'll also be greatful for an alternative solution, given a step-by-step instruction is provided.

+3  A: 

It turned out to be quite simple, much simpler than interceptors, AOP and the myriads of weirdness that can be found all over the internet. Insert this snippet (mind my little comment in the second init-param) into your web.xml to forget all your woes.

<!-- Hibernate OpenSession Filter -->
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>***WhateverTheNameOfYourSessionFactoryBeanIs***</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
sammy