Hi,
I've been trying to get the parent/child example from chapter 21 of the reference documentation working without success. It seems very straight forward so I'm probably missing something obvious but I've spent the last few hours reading the forum and searching online without finding the answer.
The mapping for the parent object is:
<class name="com.test.Parent" table="PARENTS">
<id name="id" column="parent_id">
<generator class="assigned"/>
</id>
<property name="name" not-null="true"/>
<set name="children" inverse="true" cascade="all,delete-orphan">
<key column="parent_id"/>
<one-to-many class="com.test.Child"/>
</set>
</class>
The mapping for the child object is:
<class name="com.test.Child" table="CHILDREN">
<id name="id" column="child_id">
<generator class="assigned"/>
</id>
<property name="name" not-null="true"/>
<many-to-one name="parent" column="parent_id" not-null="true"/>
</class>
I can create the objects by adding a child to the parent and saving the parent. I can even delete the children and the parent IF it has no children BUT if I try to delete a parent object that does have children I get the following error:
Duplicate key or integrity constraint violation message from server: "Cannot delete or update a parent row: a foreign key constraint fails"
I would expect Hibernate to delete the children with the parent because I have specified cascade="all,delete-orphan" but the logs show that Hibernate is not trying to delete (or even update) the child at all. The first SQL call is the delete on the Parent table and it of course fails.
I've tried a number of different settings (too many to list here) but nothing has changed this behavior.
I'm also seeing the same problem for an object with a collection of values (strings) as described in section 1.3.4 of the reference documentation.
I am using Hibernate 3.2 with MySQL 4.1 on windows.
I'd appreciate any assistance anyone could offer.
Thanks
Sam
|