Pani
Pani

Reputation: 186

Hibernate one-to-many bidirectional mapping deleting child

I have a big confusion If I am deleting child record all records are deleted including parent.Can some eone explain me how the below code is working.

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"/> 


Vendor table
Vendid vendorname
1 IFL

Customer table
custid custname custvendid
 2     XYZ      1
 3     ABC      1

Upvotes: 1

Views: 838

Answers (1)

Danilo Cianciulli
Danilo Cianciulli

Reputation: 945

You may want to remove the cascade="all" in the Customer.hbm.xml.

If you set cascade="all", then when you perform the remove operation, it is propagated to the parent object.

Please, see the Hibernate Transitive Persistence documentation for further details.

Upvotes: 2

Related Questions