springhibernateopen-session-in-view

OpenSessionInViewInterceptor in a non web application


I have to use OpenSessionInViewInterceptor in a non-web application. I have configured OSV Interceptor as follows,

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

I'm getting a Lazy load exception. Any pointers to where I'm going wrong? Is the configuration correct?

Or does a non-web app require OpenSessionInViewFilter instead of interceptor?

Thanks!


Solution

  • From the first line Java Doc of OpenSessionInViewInterceptor

    Spring web request interceptor that binds a Hibernate Session to the thread for the entire processing of the request.

    So if you do not have a Web Request because you do not have a web application, then this Interceptor does not helps you.

    The OpenSessionInViewFilter is for only Web Applications too.

    So you will need to start your Session/Transaction "By Hand", for example with @Transactional.