Hello,
I was implementing a parent-child relationship with map. But there was a problem. The index of the map was not stored in the database.
I obeyed the rules explained in the Chapter 9. Parent/Child Relationships of the Hibernate2 Documentation.
The documentation (9.2) says that the relationship between the parent/child should be specified as inverse="true" inside the parent's mapping.
If this is not specified then there will be an INSERT to create the child and an UPDATE to create the link from parent to child. So INSERT will violate the not-null constraint on the parent_id column.
But when I made inverse="false" these problems didn't occur. Moreover the former problem was resolved, namely the index of the map is stored correctly.
My code is:
Code:
Parent p = (Parent) s.load(Parent.class,pid);
Child c = new Child();
p.addChild(c);
s.save(c);
s.flush();
Mapping of the parent's map is:
Code:
<map name="children" table="children" lazy="false" >
<key column="parentId" />
<index column="i" type="integer" />
<one-to-many class="Child" />
</map>
and the association from child to parent:
Code:
<many-to-one name="parent" class="Parent" not-null="true" column="parentId" />
According to the documentation, the above code should fail. But it works correctly. What is the reason, may anybody help me to understand this?