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:
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: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