• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Hibernate lazy fetching strategy

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Hibernate session is in fact a wrapper of a JDBC connection . The detaching/reattaching is about entities.

./pope
 
Don't touch me. And dont' touch this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!