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.  [ 7 posts ] 
Author Message
 Post subject: Fetching entire object at once instead of usual lazy loading
PostPosted: Tue Jun 26, 2007 2:18 pm 
Newbie

Joined: Sun Jun 03, 2007 5:01 pm
Posts: 6
Lazy loading is a great feature, especially as many of my objects have lots of child objects to manage details, and some of them have children, or are polymorphic, or, etc... (It was the effort required in persisting some of the more complex object graphs that inspired me to finally stop messing around with hand coded JDBC nonsense and use a proper ORM).

Having Hibernate proxies automatically created to lazy load all this stuff is a pretty killer feature imho.

In some cases(*) though I want to explicitly force Hibernate to pull all the data for a specific object from the db in one shot instead of lazy-loading on demand.

Is there a simple (programmatic) way to tell Hibernate to do this when I make such a query or once I have the (root) object in hand?

That is, something like Hibernate.initialize() perhaps, but to which I can pass the root object and it will initialize everything in its graph as though I had mapped it all with lazy=false?

(*)Take for example a getFooByName() method in a FooService where the caller is outside of the session. Of course I could have the code in getFooByName go and tickle the relevant properties to force them to load, but then when I mess round with Foo.hbm.xml and set other properties as lazy, or add properties Im also going to have to go and add code in getFooByName which I am not inclined to do, and of course it has to know about what the lazy properties in the child objects are too and so on.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 4:36 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
If I get you correctly, then yes, Hibernate (like every orm, I think) has this feature.

Changing at runtime the data you wanna be retrieved is called a fetch plan.

With Criteria API, use setFetchMode(). Say you want to have parent + children although they're lazy loaded by default :
Code:
List <Parent> parents = session.createCriteria(Parent.class).setFetchMode("children").list();

This will use an outer join to retrieve parents with or without children at once.

With the HQL, it's the same principle, though not the same method :
Code:
session.createQuery("from Parent p left join fetch p.children");


HTH

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 4:57 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Duplicate, please ignore.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 11:11 am 
Newbie

Joined: Sun Jun 03, 2007 5:01 pm
Posts: 6
Thanks Baptiste,

batmat wrote:
If I get you correctly


Not quite though.

The techniques you suggested are better than what I am currently doing (that being to explicitly retrieve the lazy stuff after retrieving the root object - hence extra selects which can be avoid with a fetch plan) - but Ill still have to explicitly state which properties I want to fetch.

What Im after is the ability to fetch _all_ the lazy stuff (including any that attached child, grandchild, etc... objects might have) without having to explicitly name them in that bit of code.

Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 03, 2007 12:13 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Maybe describing a bit more precisely your use case could help here, 'cause I don't understand why you want to have a complete non-lazy (i.e. eager) mode without just putting lazy="false" in the mapping...

If you want to do it programmatically for some reason, then the only way is the one I suggested from what I know.

If you want to hide those things from someone, maybe you should just consider write some code that uses reflection and add the bits of hql/criteria needed?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject: Fetching an entire graph of objects using the Criteria API
PostPosted: Fri Sep 19, 2008 8:52 am 
Newbie

Joined: Thu Sep 18, 2008 11:14 am
Posts: 2
Location: Paris
Hi,

I also have the need to fetch a whole graph of objects, using the Criteria API.

Let's take the following example : companies have departments, each department having a manager.

From what I've understood, my need might be fullfilled by the following lines of code :

List result = sess.createCriteria(Company.class)
.add( Restrictions.like("name", "%Inc") )
.setFetchMode("departments", FetchMode.EAGER)
.createCriteria("departments")
.setFetchMode("manager", FetchMode.EAGER)
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
.list();

Iterator iter = result.iterator();
Map map = null;
Company company = null;
while ( iter.hasNext() ) {
map = (Map) iter.next();
company = (Company) map.get(Criteria.ROOT_ALIAS);
...
}

Is this correct ? If not, how can I do ?

Thanks in advance.
Regards.
Patrice.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 9:10 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Hi Patrice,

Please don't "steal" threads. When you have questions, create a new one instead of adding a question inside an existing thread.

Cheers.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.