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.