Hi all,
I'm new to Hibernate and have the following problem: Basically, all I'd like to map is a Parent/Child relationship on a legacy app. The table "Tree" looks like
---------------------------------------------------------
| id1 | id2 | name | parent_id1 | parent_id2 |
---------------------------------------------------------
where id1 and id2 is a composite key and parent_id1 and parent_id2 are foreign keys to the same table (a parent has multiple childs).
The goal is to get a Java Object tree with the root element containing all its direct children in a collection "children" (and so forth). Then I'd like to export this tree to a XML document.
So far I have a TreeId class for the composite key, a Tree class with getters and setters for treeId, name and children (collection) and this Tree.hbm.xml:
Code:
...
<class name="Tree">
<composite-id name="id" class="TreeId">
<key-property name="id1"/>
<key-property name="id2"/>
</composite-id>
<set name="children" table="Tree" inverse="true" cascade="all-delete-orphan">
<key>
<column name="parentId1" not-null="true"/>
<column name="parentId2" not-null="true"/>
</key>
<one-to-many class="Tree"/>
</set>
<property name="name"/>
</class>
...
This produces empty foreign keys on inserts. Is there any chance to make this work? If so, how would performance be?
BTW, if this table was a nested Set, could hibernate efficiently handle that? (just curious)
Thanks for all answers!
Regards,
Simon