Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Joins Performance Issues

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Arun Giridhar
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On EAGER fetch use Set as Collection instead of List for 1 to M with respect to Hibernate .
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic