I am using Hibernate 2.1.8 with the following parent child relationship:
public class Parent {
private Set<Child> children = new HashSet<Child>();
}
public class Child{
private Parent parent;
}
The Hibernate mappings look like:
<hibernate-mapping>
<class name="Parent" table="parent">
<id name="id" column="parent_id" unsaved-value="0" type="int">
<generator
class="com.mx.releasemgr.db.hibernate.HibernateIdentityGenerator" />
</id>
...
<set name="children" table="child" inverse="false" cascade="all-delete-orphan">
<key column="parent_id" />
<one-to-many class="child"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.mx.releasemgr.domain.ImplementationProjectChange" table="implementation_project_change" >
<id name="id" column="implementation_project_change_id" unsaved-value="0" type="int">
<generator class="com.mx.releasemgr.db.hibernate.HibernateIdentityGenerator"/>
</id>
<many-to-one name="parent" column="parent_id" class="Parent" not-null="true"/>
</class>
</hibernate-mapping>
If the following code is executed, the parent_id
column(s) of the child(ren) is only set to NULL, but the record is not deleted. I would like the child records to also be deleted, but can't figure it out.
parent.getChildren().clear();
session.update(parent);
How can I make the children be deleted if I update the parent with an empty collection?