aspose file tools*
The moose likes Object Relational Mapping and the fly likes OpenSessionInViewFilter and HibernateTemplate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OpenSessionInViewFilter and HibernateTemplate" Watch "OpenSessionInViewFilter and HibernateTemplate" New topic
Author

OpenSessionInViewFilter and HibernateTemplate

Brian Mulholland
Ranch Hand

Joined: Mar 12, 2009
Posts: 61
I am attempting to 'fix' and application that was built by a set of developers who were just learning Hibernate...and I know even less Hibernate. The application is filled with direct calls to DAOs rather than using services, and make extensive us of HibernateTemplate despite being hibernate 3. A co-worker and I are attempting to put OpenSessionInviewFilter in play to at least get lazy-loading, dynamic updating, and first level cache into play.

However, contrary to our expectations, I subclassed OSIVF and logged out when getSession is called and the hashCode of the object, and despite the presence of OSIVF, each service/DAO method called by the controller code is getting it's own session. Thing did improve a bit, in that once in a method marked @Transactional, it appears that within that method, sessions are being reused. But controller code transitions between pages, and has modularity, and I don't think it's reasonable to mark the controller code @Transactional (that should be done to model code).

How can I get these separate methods to behave as expected...ie using the same session for the duration of the request?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Sounds like you have your hands full

I would get rid of HIbernateTemplate if you are using Hibernate 3 or 4. I am also not a fan of OSIVF see below for my most recent post on that)
http://www.coderanch.com/t/590690/ORM/databases/eliminate-Performance-impact-hibernate#2690836

Load all the data you need when you are inside of a transaction in your service layer, that way it will be available when the transaction ends and the entity becomes detached. If you know you need the data than eagerly load it, and you wont have any lazy load exceptions.

Sessions are tied to Spring transactions I gave a bit more explanation in this thread maybe it will help you.
http://www.coderanch.com/t/590071/ORM/databases/org-hibernate-SessionException-Session-closed#2687293


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OpenSessionInViewFilter and HibernateTemplate
 
Similar Threads
Query with using Spring , EJB and Hibernate in the Application
Hibernate Entity Beans in HTTP Session
Transaction ManageMent in Spring with Hibernate
OpenSessionInViewFilter does not help, Spring + Hibernate
LazyInitializationException [spring + struts2]