Thursday 19 July 2018

Hibernate JPA Cascade Types

            In previous post, we learned the Difference between save() and persist() method in Hibernate. In this, we can discuss Cascade Types in Hibernate.

          The Hibernate JPA Cascade types are as follows,

  • PERSIST  ->  CascadeType.PERSIST  - save() or persist() operations cascade to related entities.
  • MERGE ->  CascadeType.MERGE -  related entities are merged when the owning entity is merged.
  • REFRESH ->  CascadeType.REFRESH - It is the same thing for the refresh() operation.
  • REMOVE ->  CascadeType.REMOVE -  removes all related entities association with this setting when the owning entity is delete.
  • DETACH - > CascadeType.DETACH - It detaches all related entities if a “manual detach” occurs.
  • ALL - > CascadeType.ALL - It is shorthand for all of the above cascade operations.


CascadeType.PERSIST


        CascadeType.PERSIST cascades the persist operation to all associated entities.  If we persist data in one entity then data will be saved in associated entity also. 
         For example if the entity Employee is associated with Address, if we are trying to save the data of Employee then it will save both parent(Employee) and the child entity(Address).  In Hibernate we need to call persist() method to persist the entity.

Employee.java

@Entity
@Table (name="employee")
public class Employee {
 
    @Id
    @Column(name="id")
    private int id;
 
    @Column(name="emp_name")
    private String name;
 
    @OneToMany(cascade=CascadeType.PERSIST) 
    @JoinColumn( name="address_id")
    private Set<Address> address;


    // setters and getters
}

Address.java


@Entity
@Table(name="address")
public class Address {
 
    @Id
    @Column(name="id")
    private int id;
 
    @Column(name="address_type")
    private String type;
  
    @column( name="address")
    private String address;

       
    // setters and getters
}

Below is the main Hibernate class to persist or save the above entities,

Address address = new Address();
address.setType("Permanent");
address.setAddress("At-Post-Sonyal, Sangli");

Employee emp = new Employee();
emp.setName("Kiran");
emp.setAddress(address);

session.persist(emp);  // will save both employee and address entities


CascadeType.MERGE

  
        CascadeType.MERGE cascades the merge operation to all associated entities.  If we merge data in one entity then data will be update in associated entity also.

Example:-

Employee.java,

@Entity
@Table (name="employee")
public class Employee {
 
     @Id
     @Column(name="id")
     private int id;
 
     @Column(name="emp_name")
     private String name;
 
     @OneToMany(cascade=CascadeType.MERGE) 
     @JoinColumn( name="address_id")
     private Set<Address> address;
       
     // setters and getters
}

Address Entity is as same as above in the example.

The Hibernate main class to merge the entities as below code,

Address address = new Address();
address.setType("Permanent");
address.setAddress("At-Post-Sonyal, Sangli");

Employee emp = new Employee();
emp.setName("Kiran");
emp.setAddress(address);

session.merge(emp);



CascadeType.REFRESH


         CascadeType.REFRESH cascades the refresh operation to all associated entities refresh by 
hibernate session. If one entity is refreshed, other associated entities will also be refreshed if CascadeType.REFRESH is annotated.

Example:-

Employee.java

@Entity
@Table (name="employee")
public class Employee {
 
     @Id
     @Column(name="id")
     private int id;
 
     @Column(name="emp_name")
     private String name;
 
     @OneToMany(cascade=CascadeType.REFRESH) 
     @JoinColumn( name="address_id")
     private Set<Address> address;
       
        // setters and getters
}

Address Entity is as same as above in the example.

Example of session Refresh() method:--


Address address = new Address();
address.setType("Permanent");
address.setAddress("At-Post-Sonyal, Sangli");

Employee emp = getEmployeeDetails();// getting existing employee details
emp.setAddress(address);

session.refresh(emp);

//here it will refresh the address in both tables


CascadeType.REMOVE


             CascadeType.REMOVE cascades the remove operation of entity from hibernate session to all associated entities remove. If one entity is removed, other associated entities will also be removed.
Example:--

Employee.java

@Entity
@Table (name="employee")
public class Employee {
 
     @Id
     @Column(name="id")
     private int id;
 
     @Column(name="emp_name")
     private String name;
 
     @OneToMany(cascade=CascadeType.REMOVE) 
     @JoinColumn( name="address_id")
     private Set<Address> address;
       
        // setters and getters
}

Address Entity is as same as above in the example.

Example of session remove() method:--


Employee emp = getEmployeeDetails();// getting existing employee details

session.delete(emp);

//here it will delete data in both tables


CascadeType.DETACH


         CascadeType.DETACH cascades the detach operation to all associated entities detach  from hibernate session. If one entity is detached, other associated entities will also be detached.
         For example if the entity Employee is associated with Address and we are detaching entity, both the entities Employee and Address will be detached from current session of hibernate. In Hibernate we need to call detach() method to detach the entity. 

Example:--

Employee.java,


@Entity
@Table (name="employee")
public class Employee {
 
     @Id
     @Column(name="id")
     private int id;
 
     @Column(name="emp_name")
     private String name;
 
     @OneToMany(cascade=CascadeType.DETACH) 
     @JoinColumn( name="address_id")
     private Set<Address> address;
       
        // setters and getters
}

Address Entity is as same as above in the example.

Use detach() method of session in Hibernate to detach the entity.


CascadeType.ALL


           In Hibernate there are different cascading PERSIST, MERGE, REMOVE, REFRESH, DETACH. These cascading are called by methods persist(), merge(), delete(), refresh(), detach(). In case we want to cascade in all above situation, then we need to use CascadeType.ALL. If we annotate our property in an entity by CascadeType.ALL, then for every action, cascading will be achieved. 

Example:--

Employee.java,

@Entity
@Table (name="employee")
public class Employee {
 
     @Id
     @Column(name="id")
     private int id;
 
     @Column(name="emp_name")
     private String name;
 
     @OneToMany(cascade=CascadeType.ALL) 
     @JoinColumn( name="address_id")
     private Set<Address> address;
       
        // setters and getters
}


Thanks for visiting blog......



Related Post:--
1) Difference between save() and persist() method in Hibernate
2) What is the difference between get() and load() methods in Hibernate?
3) Hibernate Criteria Queries and Examples
4) Hibernate Query Language(HQL) Examples
5) What is a Hibernate Caching ? Explain first level and second level cache in Hibernate
6) What are different states of an entity bean in Hibernate?

No comments:

Post a Comment