我正在使用具有以下父子关系的 Hibernate 2.1.8:
public class Parent {
private Set<Child> children = new HashSet<Child>();
}
public class Child{
private Parent parent;
}
Hibernate 映射如下所示:
<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>
如果执行以下代码,parent_id
则 child(ren) 的列仅设置为 NULL,但不会删除记录。我希望子记录也被删除,但无法弄清楚。
parent.getChildren().clear();
session.update(parent);
如果我用空集合更新父级,如何删除子级?