aspose file tools*
The moose likes Object Relational Mapping and the fly likes Lazy loading and Fetch strategies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Lazy loading and Fetch strategies" Watch "Lazy loading and Fetch strategies" New topic
Author

Lazy loading and Fetch strategies

Santosh Manikandan
Greenhorn

Joined: Sep 11, 2011
Posts: 9

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


Think, Learn, Teach, Invent, Innovate, Build, Deliver
Santosh Manikandan
Greenhorn

Joined: Sep 11, 2011
Posts: 9

Hey People. No responses yet. Kindly let me know if something is unclear. Thanks.
Santosh Manikandan
Greenhorn

Joined: Sep 11, 2011
Posts: 9

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.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy loading and Fetch strategies