Using: Hibernate v2.1
I have a class, X, whose objects maintain lists of child objects of the same type, X (x1.getChildren() returns List[x2, x3, ..., xn]). I have hibernate set up to maintain the sort order for the children (via the <index> sub-element to the <list>). However, since I obtain the top level X's via a query, Hibernate does not maintain the sort-order at this level.
My first thought was to just introduce a new class, Y, such that Y would have the top level X's as it's children. Unfortunately, this doesn't work because there's no table to map to Y.
My question is:
Is there a way to do what I've described without the introduction of the 'dummy' class?
I can't provide actual code snippets, so I stripped out unnecessary stuff and changed names...
DB table:
Code:
X
(
xId VARCHAR(32) NOT NULL PRIMARY KEY,
parentId VARCHAR(32) REFERENCES X (xId),
sortOrder INTEGER NOT NULL
)
Class: (This code, as presented, is buggy but I didn't want extraneous details of bounds checking to get in the way)
Code:
public class X
{
private String id;
private X parent;
private List children = new LinkedList();
public void addChild(X child)
{
child.setParent(this);
children.add(child);
}
public void bumpChild(X child, int delta)
{
int oldIndex = children.indexOf(child);
children.add(child, oldIndex + delta);
}
// appropriate getters/setters
}
Relevant portion of the Hibernate Mapping:
Code:
<class name="X" table="X">
<id name="id">
<generator class="uuid.hex"/>
</id>
<many-to-one class="X" name="parent" colum="parentId">
<list name="children" table="X" cascade="all" inverse="true">
<key="parentId"/>
<index column="sortOrder"/>
<one-to-many class="X"/>
</list>
</class>