-->
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.  [ 5 posts ] 
Author Message
 Post subject: Fetching lazy object hierarchy in one HQL statment
PostPosted: Wed May 17, 2006 6:17 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Not sure if this is possible, but...

I have a hierarchy of objects (call it Task) where each Task object can have a parent Task object (the Parent property) and a collection of child Task objects (the Children property). If the parent is null then it's a ROOT object, if it has a parent and children it's a PARENT object and if it's got a parent but no children it's a LEAF object. The Task type is lazy loaded.

My question is can you select a subset of the ROOT nodes (say the first 50) and all their child objects, and their children, their children's children (etc, etc) down the the leaf objects using HQL?

Thanks in advance to those who have a clue.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 7:51 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Quote:
My question is can you select a subset of the ROOT nodes (say the first 50) and all their child objects, and their children, their children's children (etc, etc) down the the leaf objects using HQL?


You can select the a subset of the ROOT nodes... (if you want, the first 50) and after that, you can call the "Children" relationship and that will fetch the childs... but feching all down to leafs... in just one shot... I find that hard to do...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 3:29 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Hmmm. That's what I thought. I know I can use the FETCH keyword to populate the Children collection of each of the ROOT objects, but that won't take care of the Children collections of each *those* objects.

I'm just trying to work out how to minimize round trips to the database to display a hierarchy.

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 6:11 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You could use some database-specific hierarchical SQL (like START WITH ... CONNECT BY in Oracle) in a native SQL query and get all the needed objects into memory in one roundtrip, but I think NH won't be smart enough to organize them into collections properly, so all Children collections will be left uninitialized and will cause an extra database query when accessed. I'm not so sure about this, so maybe it could actually be made to work.

In any case, the Parent properties of all objects will be set correctly after the first query though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 8:22 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Hey Sergey - I'll investigate that.

Thanks,

Symon.


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