-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Parent-child self join mapping problem
PostPosted: Mon Dec 18, 2006 5:13 am 
Newbie

Joined: Mon Dec 18, 2006 4:12 am
Posts: 1
Hibernate version: 3.2
Name and version of the database you are using: Oracle9

Hi!

I'm trying to build a parent/child hierarchy. The actual "object" is mapped to the table CI while the table CI_STRUCTURE holds the parent-child relations.

All works fine (ok, not all because then I wouldn't be writing...) and the testcase saves data to database. What is missing is the parent/child relation.

Database schema:
Image

Mapping documents:
Code:
<hibernate-mapping package="blah.blah.vo">
    <class name="CiStructure" table="CI_STRUCTURE">
        <id name="ciStructureDbId" type="integer" unsaved-value="null">
            <column name="CI_STRUCTURE_DB_ID" />
            <generator class="sequence">
                 <param name="sequence">CI_STRUCTURE_DB_ID_SEQ</param>
            </generator>
        </id>
        <version name="version" type="integer" column="VERSION"/>
       
        <many-to-one name="ci" class="Ci" not-null="true" lazy="false" cascade="save-update">
            <column name="CI_DB_ID"/>
        </many-to-one>

        <list name="children" table="CI_STRUCTURE" inverse="true" cascade="save-update" lazy="false">
             <key column="PARENT_CI_STRUCTURE_DB_ID"/>
             <list-index column="LEVEL_SORTORDER" base="1" />
             <many-to-many not-found="ignore" class="CiStructure">
                 <column name="CI_STRUCTURE_DB_ID"/>
             </many-to-many>
        </list>
        <join table="CI_STRUCTURE" inverse="true"
             <key column="CI_STRUCTURE_DB_ID"/>
             <many-to-one name="parent"not-found="ignore" class="CiStructure" cascade="save-update" lazy="false">
                 <column name="PARENT_CI_STRUCTURE_DB_ID"/>
             </many-to-one>
        </join>
    </class>
</hibernate-mapping>


Hibernate is running on a JBoss server 4.0.4.

JUnit testcase:
Code:
   public void testSave() throws Exception {
      LabRemote instance = this.home.create();
      
      List ciTypes = instance.getAllCiTypes();

      Ci ws = new Ci("Parent", (CiType)ciTypes.get(0));
      CiStructure root = new CiStructure(ws);

      Ci pp1 = new Ci("Child 1", (CiType)ciTypes.get(1));
      root.addChild(new CiStructure(pp1));

      Ci pp2 = new Ci("Child 2", (CiType)ciTypes.get(1));
      root.addChild(new CiStructure(pp2));

      instance.save(root);
   }


where

Code:
   public void addChild(CiStructure child) {
      child.setParent(this);
      this.children.add(child);
   }



Result in database:
Image

As you can see the parent column is empty and as a result the level_sortorder fails (intended to be "2" for CI_STRUCTURE_DB_ID=164)

The parent/child structure that I was intending to create is kind of collapsed without the parent/child relation!

The generated SQL (show_sql=true):
Code:
2006-12-18 09:16:03,557 INFO  STDOUT.write() 152 - Hibernate:
    select
        CI_STRUCTURE_DB_ID_SEQ.nextval
    from
        dual
2006-12-18 09:16:03,557 INFO  STDOUT.write() 152 - Hibernate:
    select
        CI_DB_ID_SEQ.nextval
    from
        dual
2006-12-18 09:16:03,567 INFO  STDOUT.write() 152 - Hibernate:
    select
        CI_STRUCTURE_DB_ID_SEQ.nextval
    from
        dual
2006-12-18 09:16:03,607 INFO  STDOUT.write() 152 - Hibernate:
    select
        CI_DB_ID_SEQ.nextval
    from
        dual
2006-12-18 09:16:03,617 INFO  STDOUT.write() 152 - Hibernate:
    select
        CI_STRUCTURE_DB_ID_SEQ.nextval
    from
        dual
2006-12-18 09:16:03,617 INFO  STDOUT.write() 152 - Hibernate:
    select
        CI_DB_ID_SEQ.nextval
    from
        dual
2006-12-18 09:16:03,627 INFO  STDOUT.write() 152 - Hibernate:
    insert
    into
        CI
        (VERSION, DENOMINATION, CI_TYPE_DB_ID, CI_DB_ID)
    values
        (?, ?, ?, ?)
2006-12-18 09:16:03,627 INFO  STDOUT.write() 152 - Hibernate:
    insert
    into
        CI_STRUCTURE
        (VERSION, NUM_OF_UNITS, CI_DB_ID, CI_STRUCTURE_DB_ID)
    values
        (?, ?, ?, ?)
2006-12-18 09:16:03,637 INFO  STDOUT.write() 152 - Hibernate:
    insert
    into
        CI
        (VERSION, DENOMINATION, CI_TYPE_DB_ID, CI_DB_ID)
    values
        (?, ?, ?, ?)
2006-12-18 09:16:03,637 INFO  STDOUT.write() 152 - Hibernate:
    insert
    into
        CI_STRUCTURE
        (VERSION, NUM_OF_UNITS, CI_DB_ID, CI_STRUCTURE_DB_ID)
    values
        (?, ?, ?, ?)
2006-12-18 09:16:03,637 INFO  STDOUT.write() 152 - Hibernate:
    insert
    into
        CI
        (VERSION, DENOMINATION, CI_TYPE_DB_ID, CI_DB_ID)
    values
        (?, ?, ?, ?)
2006-12-18 09:16:03,637 INFO  STDOUT.write() 152 - Hibernate:
    insert
    into
        CI_STRUCTURE
        (VERSION, NUM_OF_UNITS, CI_DB_ID, CI_STRUCTURE_DB_ID)
    values
        (?, ?, ?, ?)


Why is PARENT_CI_STRUCTURE_DB_ID missing in the inserts? What's wrong with my mapping? Any suggestions? All help appreciated!

Regards
//Staffan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.