These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Eager fetching for collections - beginer question
PostPosted: Mon May 17, 2010 6:05 am 
Newbie

Joined: Fri May 07, 2010 5:40 pm
Posts: 4
Hi I am new to hibernate and I am confused about fetching strategy.
I have two tables with one-to-many relationship (e.g. Person and BankAccounts). According to hibernate reference I should use setFetchMode(FetchMode.JOIN) at runtime instead changing it in xml file. But why related collection is loaded only when I use an outer join? When I use inner join I have to initialize collections explicitly in code, even if only one select is executed to retrieve object and related collection.
And another question - is there some API for criteria to set batch-select and subselect fetching at runtime?


Top
 Profile  
 
 Post subject: Re: Eager fetching for collections - beginer question
PostPosted: Mon May 17, 2010 9:52 am 
Newbie

Joined: Wed Mar 03, 2010 5:51 am
Posts: 4
yes u have FetchMode class, there we have differentmodes like
FetchMode.Default
FetchMode.Join
FetchMode.select
FetchMode.subselect

for batch size i dont know using criteria but u can specify in hbm file for collections or class
like

session.createCriteria(person.class)
.setFetchMode("accounts", FetchMode.Join));


and with in the transaction you will get the data from the table even you kept lazy="true"

_________________
dsvk


Top
 Profile  
 
 Post subject: Re: Eager fetching for collections - beginer question
PostPosted: Tue May 18, 2010 3:43 am 
Newbie

Joined: Fri May 07, 2010 5:40 pm
Posts: 4
Thanks for your reply dungakishore,

Class FetchMode has not (according to javadocs) subselect field. You have three choices: default, join, select (lazy and eager are deprecated)
According to hibernate reference fetch strategy should be left in hbm file as is and changed at runtime as needed. But I can't see api to accomplish that.
Using join strategy for optimisation has consequence in duplicated object returned by query and batch and subselect has not.

Another matter is why only left outer join cause related collection loaded eagerly (inner join not)
reffering to my example with person and accounts:
Code:
DetachedCriteria nonLazyTest = DetachedCriteria("Person.Class")
     .createCriteria("accounts", Criteria.LEFT_JOIN)
        .add(some restrictions...)

will cause accounts collection loaded in one select with persons but in example below:

Code:
DetachedCriteria nonLazyTest = DetachedCriteria("Person.Class")
     .createCriteria("accounts", Criteria.INNER_JOIN)
        .add(some restrictions...)


accounts collection need to be initalized or you will get LazyInitializationException if you try to refer accounts outside session scope


Top
 Profile  
 
 Post subject: Re: Eager fetching for collections - beginer question
PostPosted: Fri May 21, 2010 4:22 am 
Newbie

Joined: Fri May 07, 2010 5:40 pm
Posts: 4
Hi
anybody ?

Maybe I put my question another way

How in criteria API accomplish following task:

How ? in one select
when ? Eagrly

please help me


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.