Version 1

    Here's an attempt at implementing the Composite Pattern using Hibernate.

    http://c2.com/cgi/wiki?CompositePattern

    Also see this blog entry for an example using Hibernate Annotations: http://www.theresearchkitchen.com/blog/archives/57

    Hibernate Mapping File

    <class name="Site" table="Site">
    <id name="id" type="java.lang.Integer" unsaved-value="null">
    <generator class="native"/>
    </id>
    
    <version name="version" type="long"/>
    <set name="childrenSites" inverse="true" lazy="true" cascade="save-update">
    <key column="parent"/>
    <one-to-many class="Site"/>
    </set>
    <many-to-one name="parent" column="parent" cascade="save-update" class="Site"/>
    <property name="description" type="java.lang.String"/>
    <property name="namespace" type="java.lang.String"/>
    <property name="name" type="java.lang.String">
    </class>
    

     

    Mysql table generated by running net.sf.hibernate.tool.hbm2java.CodeGenerator.

    mysql> desc site;
    +-------------+-------------+------+-----+---------+----------------+
    | Field       | Type        | Null | Key | Default | Extra          |
    +-------------+-------------+------+-----+---------+----------------+
    | id          | int(11)     |      | PRI | NULL    | auto_increment |
    | version     | bigint(20)  |      |     | 0       |                |
    | parent      | int(11)     | YES  | MUL | NULL    |                |
    | description | varchar(64) |      |     |         |                |
    | namespace   | varchar(64) |      |     |         |                |
    | name        | varchar(64) |      |     |         |                |
    +-------------+-------------+------+-----+---------+----------------+
    6 rows in set (0.03 sec)
    

     

    Code fragement showing getChildrenSites() and getParent();

    List sites = session.find("FROM Site AS site");
    System.out.println("Number of sites returned: " + sites.size());
    Iterator iter = sites.iterator();
    while (iter.hasNext()) {
    Site site = (Site) iter.next();
    site.getName();
    site.getChildrenSites());
    site.getParent();
    }
    

     

    About searching:

    To make searching for it easier, here are the terms usually associated
    with each technique:
    >1) Interval method: using a low - high interval on each node such that
    >all childrens' intervals are subsets of the parent's interval.
    Nested Set

    >2) Tree-code annotation: as a tree-code attribute (e.g., "1.2.3.4.5")
    >such that you can find all children of "1.2.3" using the predicate
    >treeCode LIKE "1.2.3.%".
    Materialized Path

    Typically the weakness of a missing explosion operation in SQL is
    misused by object database vendors to sell their wares. My blog entry
    about this issue:
    http://blog.hibernate.org/cgi-bin/blosxom.cgi/2004/12/04#gettricked