-->
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: Parent child recursion.
PostPosted: Fri Apr 04, 2008 1:52 am 
Regular
Regular

Joined: Tue Apr 01, 2008 5:39 pm
Posts: 61
Hi I have parent child relationship set up where I can use
select node * from Tree
left join fetch Tree.child
where node.id = :id

I want to know if their is a code to actually also include the parent node along with all the child node include children's children's children... to because union is not supported. Is their a work around this, I want avoid using pure sql-query if possible.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 5:15 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Yes you can. I guess u are storing both the parent and the child in the same table with the child holding a reference to the parent id. In that case you can use a set in the mapping

Example:

Code:
<class name="Tree" table="Tree">
<id name="id" column="id">
</id>
<property />
.
.
.
<set name="children">
<key column = "parent_id"/>
<one-to-many class="Tree"/>
</set>
</class>


The above mapping says a class tree contains a collection of Tree objects.

So all you have to do to retrieve the entire tree to how many ever levels is a simple hql or a simple load by Id

Your code will be

Code:
session.createQuery("from Tree where id = 1 ").list();


It will return to u an array list of Tree object with id = 1. You can then get each of the tree objects within which you will have a Set of Tree objects that are its children.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 2:14 pm 
Regular
Regular

Joined: Tue Apr 01, 2008 5:39 pm
Posts: 61
sorry thats not what i meant.


lets say childX, contains child1 and child2.
childY, contains child3 and child 4.

parentNode.add(node1);
parentNode.add(node2);

parentNote.getchild should print childX, childY, child1, child2, child3, child4. in a set.

It should not return a set of set rather just single collection of all object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 8:33 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
I don't think it can work that way. And I feel its wrong to do that too. You are trying to change the object relationship itself. Ofcourse, you can use hql or native sql to do this. But you said you wanted to avoid it.

_________________
Sukirtha


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.