This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lazy loading and Fetch strategies

 
Santosh Manikandan
Greenhorn
Posts: 9
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Hibernate 3.6
User is an entity class
Contact is an entity class
User has a Set<Contact>
The relationship is uni-directional and mapped as one-to-many.

I have tried out the following lazy loading and fetch combinations. Here is a list of my understanding and actual results.



Here are a few questions I have:
1. Is my understanding correct ?
2. With session.get() when lazy=true, fetch=subselect why does not Hibernate execute a subselect ? I guess this is because it is absolutely un-necessary. Am I correct ?
3. With session.get() when lazy=false, fetch=subselect why does not Hibernate execute a subselect ? It should execute one here but it does not. I wonder why ?
4. With session.createQuery() when lazy=true, fetch=join why does Hibernate lazy load ? It did not do this earlier with session.get()
5. With session.createQuery() when lazy=false, fetch=join why does not Hibernate use a join ?

Thanks in advance
 
Santosh Manikandan
Greenhorn
Posts: 9
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey People. No responses yet. Kindly let me know if something is unclear. Thanks.
 
Santosh Manikandan
Greenhorn
Posts: 9
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok people, after much digging this is what I can conclude. Just wanted to share my spoils with you all. If anyone feels they can add more to this, you are most welcome. Let me start with a sample data set.



Q2. With session.get() when lazy=true, fetch=subselect why does not Hibernate execute a subselect ? I guess this is because it is absolutely un-necessary. Am I correct ?
A2. Yes a subselect is absolutely un-necessary here. Here is what the subselect might look like


I do not see any benefit of executing a sub-select here over the current strategy of simply executing a seperate select for contact records. Infact, a subselect might be an un-necessary performance drain.

Q3. With session.get() when lazy=false, fetch=subselect why does not Hibernate execute a subselect ? It should execute one here but it does not. I wonder why ?
A3. Ok. Again here is what the sub-select might look like (exactly similar to the one above with some other id)


As you can see this will not yield any records since USER_ID=3 does not have any contact records. That defeats the whole purpose of doing session.get() for a User record wherein get() will return null inspite of having a valid User record in the table. So again, a seperate select for contact records is the only way out.

Q4. With session.createQuery() when lazy=true, fetch=join why does Hibernate lazy load ? It did not do this earlier with session.get()
Q5. With session.createQuery() when lazy=false, fetch=join why does not Hibernate use a join ?
Ans. My current understanding says that this maybe simply because Hibernate does not want to end up firing a join which selects a huge data set (comprising all User records and their Contact records) and loading a huge collection in-memory.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic