我有一个很大的困惑如果我删除子记录,所有记录都被删除,包括父。可以解释一下以下代码是如何工作的。
public class Vendor {
private int vendorId;
private String vendorName;
private Set children;
}
public class Customer {
private int customerId;
private String customerName;
private Vendor parentObjets;
}
class Test {
Customer customer=(Customer)session.get(Customer.class,1);
session.delete(customer);
}
Vendor.hbm.xml
<hibernate-mapping>
<class name="str.Vendor" table="vendor">
<id name="vendorId" column="vendid" />
<property name="vendorName" column="vendname" length="10"/>
<set name="children" cascade="all" inverse="true">
<key column="custvendid" />
<one-to-many class="str.Customer" />
</set>
</class>
</hibernate-mapping>
Customer.hbm.xml
<hibernate-mapping>
<class name="str.Customer" table="customer">
<id name="customerId" column="custid" />
<property name="customerName" column="custname" length="10"/>
<many-to-one name="parentObjets" column="custvendid" cascade="all" not-
null="true"/>
供应商表
Vendid vendorname
1 IFL
Customer table
custid custname custvendid
2 XYZ 1
3 ABC 1
答案 0 :(得分:2)
您可能希望删除cascade="all"
中的Customer.hbm.xml
。
如果设置cascade="all"
,则在执行删除操作时,会将其传播到父对象。