jQuery in Action, 2nd edition*
The moose likes Object Relational Mapping and the fly likes Hibernate lazy fetching strategy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate lazy fetching strategy" Watch "Hibernate lazy fetching strategy" New topic
Author

Hibernate lazy fetching strategy

Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Hi there,

I'm building a Swing application that gets objects from a database using Hibernate.
The user gets a list of all Items in the db. If he chooses an Item he sees a list of all the Bids that are connected to this Item (one-to-many). The Bids are lazy-fetched since there are a lot of Items and the user only needs to see the bids of a few of the Items.

Now I would like to know how to handle the session-side of this story.
I start a session when I load all of the Items, I need to keep this session open since the Bids are lazy fetched. Since it could take ages till the user is done with that session, this is not an option (connection is closed after x seconds). So I need to reattach the Item to a new session, get the Bids and close the session again.

But this implies that I add some hibernate specific code in my GUI logic.. (Items are in a JComboBox, add actionlistener (with the Hibernate session.lock and session.close code), show Items in list).
Is there another (nicer) way so I can keep this code in my db access layer and that I could just call selectedItem.getBids()?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Sebastian I think this is more a design decision than a Hibernate usage scenario. I suggest you to take a look at DAO pattern and also at the patterns related to large collection fetching. They will give an idea how your application should look like.

./pope


blog - InfoQ.com
Sushil Kumar
Greenhorn

Joined: Nov 10, 2004
Posts: 3
A hibernate session can span many connection open and close cycles.
you need to modify your code to "detach" and "reattach" your session object to connections.

Hope this helps
cheers
sushil
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
The Hibernate session is in fact a wrapper of a JDBC connection . The detaching/reattaching is about entities.

./pope
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate lazy fetching strategy
 
Similar Threads
Please Help - Remove only the duplicated ITEM and NOT the original item that was duplicated.
Is it possible/practical to use lazy loading with servlets and JSPs?
Domain Model of Java Persistent with Hibernate
please help me avoid n+1 select hibernate
Best design for a shopping cart