-->
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.  [ 11 posts ] 
Author Message
 Post subject: how to find all its child and grandchild...?
PostPosted: Fri Sep 05, 2003 3:27 am 
Regular
Regular

Joined: Wed Sep 03, 2003 8:04 am
Posts: 55
The Location has a many-to-many relation with itself as below:

<class name="com.omnet.bean.Location" table="location">
<id name="locId" type="integer">
<column name="loc_id" sql-type="int" not-null="true"/>
<generator class="native"/>
</id>
<property name="locName" type="string">
<column name="loc_name" sql-type="varchar(50)" not-null="true"/>
</property>
........
<bag name="parent" lazy="true" table="location_relation">
<key column="child_id"/>
<many-to-many column="parent_id" class="com.omnet.bean.Location"/>
</bag>
</class>

the association table,location_relation, is as below:
CREATE TABLE device_relation (
child_id int(11) NOT NULL
parent_id int(11) NOT NULL,
) TYPE=InnoDB;

the following is sample data in the association table:
....
22-----------5
24-----------5
23-----------22
......

then how to find 5's child and grandchild ?

the right answer is 22,24,23.


regards !


Top
 Profile  
 
 Post subject: Up to now i just know....
PostPosted: Fri Sep 05, 2003 4:59 am 
Regular
Regular

Joined: Wed Sep 03, 2003 8:04 am
Posts: 55
...
Query q = sess.createQuery("select loc from com.omnet.bean.Location loc join loc.parent p where p.locId=:id");
q.setString("id",5);


it just returns 22,24.


I know it is a recursive question . Would you mind giving any suggestion ?


thanks in advance !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 6:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
If you are only worried about direct children and grandchildren (i.e., not great-grandchildren), then you can use:
Code:
select loc
from com.omnet.bean.Location loc
    join loc.parent p
where p.locId = :id
  or p.parent.id = :id


Top
 Profile  
 
 Post subject: but the great-grandchild do exists :(
PostPosted: Fri Sep 05, 2003 6:52 am 
Regular
Regular

Joined: Wed Sep 03, 2003 8:04 am
Posts: 55
thanks for your reply !

the hierarchy level is un-determined !


regards!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 7:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
the hierarchy level is un-determined !


This cannot be done in ANSI SQL.


Top
 Profile  
 
 Post subject: then i need some special way ?!
PostPosted: Fri Sep 05, 2003 7:07 am 
Regular
Regular

Joined: Wed Sep 03, 2003 8:04 am
Posts: 55
Maybe i need to write a method in java to do the recursive job :(

i will try and hope there is another way to do it.


thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 9:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The database you are using may be able to support this. Oracle, for example, has the CONNECT BY clause for hierarchical queries. If the database you use supports this, you may be able to do it using a native sql query in hibernate.

Aside from that, a nice way is to map both parent and children in your class. Then simply load the one you are interested in finding the chilren and grandchildren for and recursively loop over the mapped collection of chilren...


Top
 Profile  
 
 Post subject: we are using MySQL...
PostPosted: Fri Sep 05, 2003 9:35 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 8:04 am
Posts: 55
>>The database you are using may be able to support this. Oracle, for example, has the CONNECT BY clause for hierarchical queries. If the database you use supports this, you may be able to do it using a native sql query in hibernate.

It seems MySQL doesnot support hierarchical queries.


>>Aside from that, a nice way is to map both parent and children in your class. Then simply load the one you are interested in finding the chilren and grandchildren for and recursively loop over the mapped collection of chilren...

i feel that just map parent we can do the recursively loop too. would you mind show me an example ?



thanks in advance !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 05, 2003 9:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Code:
<class name="Location" table="location">
    <id name="locId" type="integer">
        <column name="loc_id"/>
        <generator class="native"/>
    </id>
    <property name="locName" type="string">
        <column name="loc_name"/>
    </property>
    ...
    <bag name="children" lazy="true" inverse="true" table="location_relation">
        <key column="parent_id"/>
        <many-to-many column="child_id" class="Location"/>
    </bag>
    <bag name="parent" lazy="true" table="location_relation">
        <key column="child_id"/>
        <many-to-many column="parent_id" class="Location"/>
    </bag>
</class>


Then you can just recurse over the elements in the children List.
Code:
Location location = (Location)session.load( Location.class, new Integer(5) );
List descendants = new ArrayList();
recurseChildren( location, descendants );

private void recurseChildren( Location location, List descendantList )
{
    if (location == null || location.getChildren() == null)
        return;
    for (Iterator itr = location.getChildren().iterator(); itr.hasNext(); )
    {
        final Location child = (Location)itr.next();
        descendantList.add( child );
        recurseChildren( child );
    }
}


Also,
Quote:
i feel that just map parent we can do the recursively loop too

How? Having the collection of parents mapped would only allow you to navigate through ancestors, not descendants.


Top
 Profile  
 
 Post subject: great !
PostPosted: Fri Sep 05, 2003 10:29 pm 
Regular
Regular

Joined: Wed Sep 03, 2003 8:04 am
Posts: 55
Up to now your solution is best .


thanks !


Top
 Profile  
 
 Post subject: Re: how to find all its child and grandchild...?
PostPosted: Thu Feb 17, 2011 12:30 pm 
Newbie

Joined: Mon Jan 24, 2005 8:33 am
Posts: 2
Location: Scotland
I have been coming up with the same issue using GORM/Grails to get a list of objects associated with nodes in an graph of categories and the best solution I came up with was a recursive loop through identical to the above, I don't think there is another way to do this?


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