This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

OpenSessionInViewFilter not working

Dec 14th, 2005, 04:43 PM

I use Spring 1.2.6 along with Hibernate 3.0.5 on WSAD 5.1.2. I want to load an object "A" with many-to-one relationship of "B". I know Hibernate 3 comes with lazy-loading="true" by default, and it always returns me proxies instead of real objects. I tried to use the OpenSessionInViewFilter to solve the problem but failed. Following are my codes:

I could get everything to work if I only loaded Data object by using HibernateTemplate.get() (**notice that even this simple loading wouldn't work if I used HibernateTemplate.load()), but I got

org.hibernate.LazyInitializationException could not initialize proxy - the owning Session was closed

if I want to load Data along with Reason by using <many-to-one> relationship. I got the error on the UI tier when JSP tried to display information of Reason object. Why was this happening since I used OpenSessionInViewFilter? Shouldn't the connection not be closed and still accessible by the JSP tags (such as <bean:write> from Struts) because the response hasn't been sent yet?

Also why didn't HibernateTemplate.load() work with this simple scenario?

Comment

I noticed another thing. When I set HibernateTemplate.setAllowCreate() to false in my DAO, I got different exception.

E SRVE0026E: [Servlet Error]-[No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here]: java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Does this mean that my OpenSessionInViewFilter did not create a new session as it should in the first place? Can somebody please give me a hint?

dsyliu, check out the javadoc from OpenSessionInView and the forums. The javadoc explains the use case for this class and what approaches you should take. In short, OSIV expects a session to be bind to the thread and should be allowed to create one in case it doesn't find any (the most common case). Moreover, normally you should use the transaction manager in order to persist the changes if you want to - that's a decision which belongs to the manager not to the OSIV. However you can extend OSIV to plug in your own strategy - again all this info is found inside the javadocs.

What I noticed is that the Session object (outSession) created by the filter is different from the Session object I got in my DAO (I used debugger and memory address is different). My guess is that because the HibernateTemplate in my DAO cannot find the session opened by this filter, so it creates its own and closes the session right after retrieving the result. In other words, the session created by the OpenSessionInViewFilter has never been bound and passed to the HibernateTemplate. Is this a reasonable guess? Or I totally misunderstood the process?

Usually if this happens then you have more then one session factory/transaction manager. The OSIVF should bind the session to the thread - you can gain access to it easily though SessionFactoryUtils.
Btw, what is your code suppose to do? - you just seem to return getSession without doing any logic.