• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate recursive mapping problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic