posted 10 years ago
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.
Think, Learn, Teach, Invent, Innovate, Build, Deliver