java - Lazy loading and Fetch strategies -
i using hibernate 3.6
user entity class
contact entity class
user has set<contact>
relationship uni-directional , mapped one-to-many.
have tried out following lazy loading , fetch combinations. here list of understanding , actual results
with session.get(user.class, <some userid>) or session.load(user.class, <some userid>)
lazy fetch result * true join ignores lazy loading * 1 select retrieving user , contacts left outer join * select 1 select user record * 1 select contacts of user * subselect 1 select user record * 1 select contacts of user * false join 1 select retrieving user , contacts left outer join * select 1 select user record * 1 select contacts of user * subselect 1 select user record * 1 select contacts of user
with session.createquery(from user)
lazy fetch result * true join 1 select user records * 1 select each user record retrieve contacts * respect lazy loading * probable n + 1 * select 1 select user records * 1 select each user record retrieve contacts * probable n + 1 * subselect 1 select user records * 1 sub-select retrieve contact records in 1 go * false join 1 select user records * 1 select each user record retrieve contacts * probable n + 1 * select 1 select user records * 1 select each user record retrieve contacts * probable n + 1 * subselect 1 select user records * 1 sub-select retrieve contact records in 1 go
here few questions have:
- is understanding correct ?
- with session.get() when lazy=true, fetch=subselect why not hibernate execute subselect ? guess because absolutely un-necessary. correct ?
- with session.get() when lazy=false, fetch=subselect why not hibernate execute subselect ? should execute 1 here not. wonder why ?
- with session.createquery() when lazy=true, fetch=join why hibernate lazy load ? did not earlier session.get()
- with session.createquery() when lazy=false, fetch=join why not hibernate use join ?
thanks in advance
ok people, after digging can conclude. wanted share spoils all. if feels can add more this, welcome. let me start sample data set.
user table -------------------------------------------------- user_id username -------------------------------------------------- 1 user 1 2 user 2 3 user 3 contact table ------------------------------------------------------------------------------------------------------------------- contactid title firstname lastname city country email user_id ------------------------------------------------------------------------------------------------------------------- 1 mr. clark kent new york united states man-of-steel@anywhere.earth 1 2 mr. hank ketcham manhattan united states dennis-mitchell@somewhere.us 2 3 mr. tony stark malibu united states iron-man@anywhere.earth 2 4 mr. bruce wayne gotham united states dark-knight@gotham.us 2
q2. session.get() when lazy=true, fetch=subselect why not hibernate execute subselect ? guess because absolutely un-necessary. correct ?
a2. yes subselect absolutely un-necessary here. here subselect might
select u.user_id user1_1_, u.username username1_, c.user_id user15_1_1_, c.contactid contactid1_, c.contactid contactid0_0_, c.title title0_0_, c.firstname firstname0_0_, c.lastname lastname0_0_, c.city city0_0_, c.country country0_0_, c.email email0_0_, user u,contact c u.user_id = 2 , c.user_id in ( select user_id user user_id = u.user_id )
i not see benefit of executing sub-select here on current strategy of executing seperate select contact records. infact, subselect might un-necessary performance drain.
q3. session.get() when lazy=false, fetch=subselect why not hibernate execute subselect ? should execute 1 here not. wonder why ?
a3. ok. again here sub-select might (exactly similar 1 above other id)
select u.user_id user1_1_, u.username username1_, c.user_id user15_1_1_, c.contactid contactid1_, c.contactid contactid0_0_, c.title title0_0_, c.firstname firstname0_0_, c.lastname lastname0_0_, c.city city0_0_, c.country country0_0_, c.email email0_0_, user u,contact c u.user_id = 3 , c.user_id in ( select user_id user user_id = u.user_id )
as can see not yield records since user_id=3 not have contact records. defeats whole purpose of doing session.get() user record wherein get() return null inspite of having valid user record in table. again, seperate select contact records way out.
q4. session.createquery() when lazy=true, fetch=join why hibernate lazy load ? did not earlier session.get()
q5. session.createquery() when lazy=false, fetch=join why not hibernate use join ?
ans. current understanding says maybe because hibernate not want end firing join selects huge data set (comprising user records , contact records) , loading huge collection in-memory.