user3094331
user3094331

Reputation: 564

How cascade works in bidirectional relaltionalship

I just wanted to know how the below code is working.If we have cascade=all in both parent and child side.

Entities:

public class Vendor { 
    private int vendorId;
    private String vendorName;
    private Set children;
}

public class Customer {

    private int customerId;
    private String customerName;
    private int forevenId;
    private Vendor parentObjets;
 }   

Mappings:

<hibernate-mapping>
    <class name="str.Customer" table="customer">     
        <id name="customerId" column="custid"  />
        <property name="customerName" column="custname" length="10"/>
        <property name="forevenId" column="forevenid"  insert="false" />     
        <many-to-one name="parentObjets" column="PrentsIds" cascade="all"/>     
    </class>
</hibernate-mapping>
<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="forevenid" />
           <one-to-many class="str.Customer" /> 
       </set> 
   </class>
</hibernate-mapping>

Test Class:

First Question: how the other child objects are inserting .Even though I am saving the single child object.

class vendor{
    //vendor
    Vendor v =new Vendor(); 
    v.setVendorId(101);
    v.setVendorName("java4s");  
    //customer   
    Customer c1=new Customer(); 
    c1.setCustomerId(504);
    c1.setCustomerName("customer4");     
    Customer c2=new Customer(); 
    c2.setCustomerId(505);     
    c2.setCustomerName("customer5");   
    Customer c3=new Customer(); 
    c3.setCustomerId(506);
    c3.setCustomerName("customer6");     
    // one-to-many
    Set s=new HashSet(); 
    s.add(c1);
    s.add(c2);
    s.add(c3);      
    v.setChildren(s);
    // many-to-one  

   c1.setParentObjets(v);
   c2.setParentObjets(v);
   c3.setParentObjets(v)
    session.save(c1);
}

Second question : how the parent and other child objects are deleting. If I am deleting a single child object. And also why hibernate is considering cascade=all at parent side in child deleting.

 Object obj=session.get(Customer.class,504);
 Customer cust=(Customer)obj;
 Session.delete(cust);

Upvotes: 1

Views: 72

Answers (2)

Narendra Pandey
Narendra Pandey

Reputation: 436

First Question -

Answer - Due to cascade all present at Vendor side.Cascade will be performed while you save customer object, internally vendor object will also be called for persistence.Your vendor object holds three customer entity, which will stored as well. Read this for more clearance.

Second Question -

Answer- As you have wrote cascade all ="true" over your child, It acts as a parent and hibernate deletes the corresponding entries in Vendor table.

Cascade all = "true" should be used at parent side(It won't throw an error at child side though).

Upvotes: 0

Zeromus
Zeromus

Reputation: 4532

for the first question: well you saved all the child object in the set

Set s=new HashSet(); 
s.add(c1);
s.add(c2);
s.add(c3);      
v.setChildren(s);

you have cascade all on both side of the relationship so when you save the single customer it will save the vendor and all the customer in the set

second verse same as the first: you got cascade all so it will delete vendor and all his associated objects when you call the delete on even a single child.

try to change the cascade on the vendor and see if it works as you intended

Upvotes: 1

Related Questions