This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OpenSessionInViewFilter and HibernateTemplate" Watch "OpenSessionInViewFilter and HibernateTemplate" New topic

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 using the same session for the duration of the request?
Bill Gorder

Joined: Mar 07, 2010
Posts: 1648

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)

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.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link:
subject: OpenSessionInViewFilter and HibernateTemplate