Using the OpenSessionInViewInterceptor for Spring + Hibernate3
Posted January 21st, 2008 by Paul CoddingIf 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.
6 Responses to “Using the OpenSessionInViewInterceptor for Spring + Hibernate3”
August 13th, 2008 at 11:03 am
I tried every thing which you mentioned in the above article and I tested it but I am still getting that exception
April 22nd, 2009 at 6:18 am
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.
September 23rd, 2009 at 12:14 pm
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!
September 25th, 2009 at 4:55 am
web.xml filter is easier to implement.
August 20th, 2010 at 12:03 am
great,,,, use full to build ma web site…. thanks !!!!!!!!!!!!
September 5th, 2010 at 2:13 am
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