I've created this blog to catalog software ideas, code examples and everything in between. I've been in the industry for over 3 years and have worked on a variety of projects from Identity and Access Management, to Web Application Security, to Java/Ruby application development.

Read More

Using the OpenSessionInViewInterceptor for Spring + Hibernate3

If you’re like me and have written Spring web applications using Hibernate for persistence, you’ve probably seen this error when using FetchType.LAZY in your @OneToMany annotation: “failed to lazily initialize a collection of role: your.Class.assocation no session or session was closed.”

If you are using lazy loading rather than eager loading for obvious reasons, and you’re trying to access associated objects in your view code, this error can be a bit difficult to resolve. Luckily there is the OpenSessionInViewInterceptor, or the OpenSessionInViewFilter. These solutions will keep the hibernate session open long enough for the view to render what is needed before it is closed, thus allowing you to access your lazy loaded associations without raising an exception. The detail of what this functionality provides can be found on the Hibernate website here.

Now, how does one get this to work in their application? Well, I’ve chosen to illustrate the basic steps necessary to implement the OpenSessionInViewInterceptor. You will need to modify 2 files. These being your applicationContext.xml, and YourApplication-servlet.xml files. We’ll start with the applicationContext.xml file. You will need to place the following snippet after you’ve defined your sessionFactory bean.

<bean id="openSessionInViewInterceptor"
    class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    <property name="sessionFactory">
        <ref local="sessionFactory"/>
    </property>
    <property> name="flushModeName">
        <value>FLUSH_AUTO</value>
    </property>
</bean>

What does the FLUSH_AUTO do? Here is a quote from the javadoc for the interceptor:

“This interceptor will by default not flush the Hibernate Session, with the flush mode being set to FlushMode.NEVER. It assumes that it will be used in combination with service layer transactions that handle the flushing: the active transaction manager will temporarily change the flush mode to FlushMode.AUTO during a read-write transaction, with the flush mode reset to FlushMode.NEVER at the end of each transaction.”

We use the FLUSH_AUTO to automatically flush the session in this case because I am not using transactions.
Now comes the edit to your YourApplication-servlet.xml file, or whatever file you’ve used to define your URL Mappings. Add the following snippet to your URL mapping bean.

<property name="interceptors">
  <list>
    <ref bean="openSessionInViewInterceptor" />
  </list>
</property>

And that’s that, redeploy and you will no longer see that dreaded exception.

Tags: ,

Filed under:Java, Software

6 Responses to “Using the OpenSessionInViewInterceptor for Spring + Hibernate3”

  • smannem Says:

    I tried every thing which you mentioned in the above article and I tested it but I am still getting that exception

  • Thierry BENDA Says:

    Hi Paul and others,

    Usefull and used in every webapp which needs persistance units i’ve done by the past few years.

    If you have an exception after having defining correctly your “openSessionInViewInterceptor” bean (and linked it to your urlMapping), you should consider looking for a mistake into your hbm.xml files.

    Bye.

  • Paul Bourdeaux Says:

    Great blog on setting this up correctly. There is a LOT of information about needing to use the OpenSessionInView strategy with Spring and Hibernate… but very little information about HOW to use it.

    Thanks!

  • Ashitkin Alexander Says:

    web.xml filter is easier to implement.

  • manoj Says:

    great,,,, use full to build ma web site…. thanks !!!!!!!!!!!!

  • Aaron Dopko Says:

    I could see that you’re an expert in your subject! I’m launching a brand new web page quickly and hey these details will be extremely useful for me man. Thanks in your support and I wish you success! =)

Leave a Reply