• 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 Joins Performance Issues

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently looking into some legacy code which uses Hibernate as the persistence layer. There is an entity say called MyEntity that has multiple joins to several other entities. Each of those entities in turn has its own associations. Now when I do a findOne using the id for the MyEntity, the Hibernate generates the SQL and eventually it just crashes the complete application. I tried to log the hibernate generated sql query so that I can run it against the database directly.

To my surprise this query just runs forever! It is a pretty huge query. It is running for the past 1 hour and still running! I can post it here if somebody could help me with it?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm expecting a single result, but what I get to see by running the sql directly on the database is that I get a collection of identical results from which in my application layer the first one is selected. How can I instruct Hibernate to just select only one row when it tries to do the sql query?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing a bit, but it sounds like your Hibernate is not lazy loading.

For the query, if Hibernate has been asked to get everything back in one go, then it will have to get multiple rows for any one-to-many relationships.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In all of the associations, we have asked it load everything EAGAR. As I mentioned that this is a legacy code base, I'm not sure what ripple effects would await me if I were to change the fetch type of certain associations! A shitty situation to be in!
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate has been asked to load everything, so that's what it is doing.
You either have to put up with the performance, or see what effect lazy loading has.

Sounds to me like now is the time for a change...
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately to change something on this legacy code base would not be supported. So the only alternative is to do a workaround! For the moment, I was able to get rid of the problem by using Hibernate's FetchMode. So I do a FetchMode.SELCET on all the joins. A quick regression test revealed that the problem is gone! I hope that this does not run into other issues. Fingers crossed!
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So which bit is the legacy code, if you're able to change the FetchMode but not the lazy loading mode?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with the Lazy loading mode is that the call to one of those Lazy variables might happen in a UI layer and by the time this entity is returned to the UI, there might not be any hibernate session in view. Since this is not a Java EE app, I cannot think of using the Open Session in View filter.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On EAGER fetch use Set as Collection instead of List for 1 to M with respect to Hibernate .
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Giridhar wrote:On EAGER fetch use Set as Collection instead of List for 1 to M with respect to Hibernate .



We do have that already!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic