Member Menu
 
 Monthly JBoss newsletter:
 
Java Persistence with Hibernate
CaveatEmptor

Composite Pattern

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.researchkitchen.co.uk/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();
}

  NEW COMMENT

Search by subsection 06 Dec 2003, 01:14 pwibbele
I've been playing with a similar structure for another purpose, but 
have been wondering what the best way to perform a search under a 
given point in the tree is.  I can see a few:
1)  Use Lucene and on creation of a site, add all of it's parent sites 
as metadata to lucene.  Unfortunately, this means moving all the 
metadata when a node or site is relocacted, including adjusting all 
child nodes when moving a mid-level node.  Additionally, it requires a 
series of traversal steps to find all of the parents, and this may be 
a series of queries to the database.
2)  Store as metadata on the object/table the parent sites of each 
node.  Again, on moving nodes, you have to readjust your metadata.
3)  Perform all of your searches by selecting the node under which you 
want to see the results and traverse.  Unfortunately here, it's harder 
to do a query that returns these results and includes an "and".  
e.g. "All sites under site X, and with characteristic Y.
4)  Limit the heirarchy to one level deep, and thus every child has 
only one ancestor.  While this is restrictive, for some functions, it 
is suitable.

Others?
 
Multi-level hierarchies 18 Jan 2005, 17:56 gschadow
There are two well known ways to deal with searching in multiple level 
hierarchies without having to traverse all the links.

1) Interval method: using a low - high interval on each node such that 
all childrens' intervals are subsets of the parent's interval. 

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.%".
 
Re: Multi-level hierarchies 14 Jul 2005, 06:54 christian
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
 
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]