• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate lazy fetching strategy

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Hibernate session is in fact a wrapper of a JDBC connection . The detaching/reattaching is about entities.

./pope
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic