Joined: Fri Apr 18, 2008 11:26 am Posts: 11 Location: London
|
I got one-to-many relationship with Parent -> Child. Default cascade="save-update" in parent.
Parent.getChilds.add(Child1) Parent.getChilds.add(Child2)
session.save(Parent)
Parent.getChilds.add(Child3)
session.update(Parent)
This works fine. But if I flush it before adding child3 then it creates new child3 but sets child1 & child2 parent to be null !!
So the following code removes the link from the parent.
Parent.getChilds.add(Child1) Parent.getChilds.add(Child2)
session.save(Parent) session.flush() Parent.getChilds.add(Child3)
session.update(Parent)
Though the existing childs are not removed from the collection still hibernate tries to remove the link between parent and child !
I could fix this by making it to bi-directional and inverse="true" but due to requirement I can't do that. I will be happy if someone figures out whats happening here rather than giving alternate solutions.
Have added the mapping :
Parent Mapping
<hibernate-mapping default-cascade="save-update"> <class name="Parent" table="parent" dynamic-insert="false" dynamic-update="false"> <cache usage="nonstrict-read-write" /> <id name="id" type="java.lang.Long" unsaved-value="null"> <column name="ID" sql-type="NUMBER(19)"/> <generator class="native"> </generator> </id> <version name="version" type="int" column="version"/> <set name="childs" lazy="true" fetch="select" inverse="false"> <cache usage="nonstrict-read-write" /> <key foreign-key="parent_CONTROC"> <column name="parent_FK" sql-type="NUMBER(19)"/> </key> <one-to-many class="Child"/> </set>
Child Mapping
<hibernate-mapping default-cascade="save-update"> <class name="Child" table="child" dynamic-insert="false" dynamic-update="false"> <cache usage="nonstrict-read-write" /> <id name="id" type="java.lang.Long" unsaved-value="null"> <column name="ID" sql-type="NUMBER(19)"/> <generator class="native"> </generator> </id> <version name="version" type="int" column="version"/>
Thanks K
_________________ Kumar
Last edited by rkumaresh on Tue May 26, 2009 12:12 pm, edited 1 time in total.
|
|