I'm writing this code in the window so there may be some errors, but you'll get the idea.
What you are describing is pretty much a "tree", as a node (parent) has a set of children (of same type) - this should help you get started:
Code:
package com.tim;
/**
* A tree item has a set of tree items
* You can go down the tree, or up the tree (e.g. bi-navigatable)
*/
public class TreeItem {
Long id;
String name;
Set<TreeItem> children; // this is it's children
TreeItem parent; // and a link to go back up the tree
// do the getters and setter
}
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.tim.TreeItem" table="Tree_Item">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String" column="name"/>
<many-to-one name="parent" column="parent_id"
class="com.tim.TreeItem"
not-found="ignore"/> <!-- needed because the top level item will not have a parent-->
<set name="children" lazy="true" inverse="true">
<key column="parent_id"/>
<one-to-many class="com.tim.TreeItem"/>
</set>
</class>
</hibernate-mapping>
The DB table Tree_Item therefore will have a id (bigdecimal, Primary Key), name (varchar) and parent_id (foreign key to id and also nullable)
Hope this helps mate - as I said it was hand typed so may have errors. Note that the children are got lazily - this is kindof a must as if it's a big tree you don't want to load the whole lot in one go I suppose. Thus remember that calls to getChildren() will require the session to be open.
:o)