• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate recursive mapping problem

 
Staffan Rehnberg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rangers!

I have a hard time to figure out how to perform a specific mapping. Hope someone can help me.

It's about a recursive object structure (named "Ci") that contains a List of children (also Ci:s) and a parent (also Ci-object). To handle this recursive structure in the database we have created a helper table ("CI_STRUCTURE"). Root nodes in the structure are identified by having PARENT_INTERNAL_ID set to -1 in the helper table.

Now to the problem. When creating a new root node, how should I map/code this to create one new row in CI with the given Ci-object and one new row in CI_STRUCTURE having PARENT_INTERNAL_ID set to -1? Setting the Ci-objects parent property to null does not make me happy.



<class name="Ci" table="CI" lazy="false">
<id name="internalCiId" type="integer" unsaved-value="null">
<column name="INTERNAL_CI_ID" />
<generator class="sequence">
<param name="sequence">INTERNAL_CI_ID_SEQ</param>
</generator>
</id>
.
. properties
.
<!-- Child CI:s of this CI -->
<list name="children" table="CI_STRUCTURE">
<key column="PARENT_INTERNAL_CI_ID" />
<list-index column="LEVEL_SORTORDER" base="1"/>
<many-to-many not-found="ignore" class="Ci">
<column name="INTERNAL_CI_ID"/>
</many-to-many>
</list>

<!-- Parent CI for this CI -->
<join table="CI_STRUCTURE" inverse="true" optional="true">
<key column="INTERNAL_CI_ID"/>
<many-to-one name="parent" not-found="ignore" class="Ci" cascade="save-update">
<column name="PARENT_INTERNAL_CI_ID"/>
</many-to-one>
</join>
</class>

Sample data for table CI
INTERNAL_CI_ID DENOMINATION
1 abba
2 babba
3 sdfadsf
4 erer
5 sdfsa
6 aasfgd



<class name="se.wmdata.vo.CiStructure" table="CI_STRUCTURE">
<id name="internalCiId" >
<column name="INTERNAL_CI_ID" />
<generator class="assigned" />
</id>
<property name="parentInternalCiId" >
<column name="PARENT_INTERNAL_CI_ID" not-null="true" />
</property>
<property name="levelSortorder" >
<column name="LEVEL_SORTORDER" not-null="true" />
</property>
</class>

Sample data for table CI_STRUCTURE
INTERNAL_CI_ID PARENT_INTERNAL_CI_ID LEVEL_SORTORDER
1 -1 1
2 1 1
3 2 1
4 3 1
5 4 1
6 2 2
[ September 27, 2006: Message edited by: Staffan Rehnberg ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic