Hibernate CascadeType Tutorial Example - Java @ Desk

Sunday, June 14, 2015

Hibernate CascadeType Tutorial Example

In Hibernate 4, parent child relationship is defined either using OneToMany or ManyToOne or ManyToMany. But how to update, save or delete a child when the parent performs either update, delete or save operation.

org.hibernate.annotations.Cascade; and org.hibernate.annotations.CascadeType; enables the child persistance as soon as parents performs some operation.

There are various CascadeType in Hibernate like None, Save_Update, Delete, etc. We will understand one by one using Annotation example.

Consider below 2 classes. Person class can have multiple Address. There is One To Many Relationship
package com.pojo;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name = "Person")
public class Person implements java.io.Serializable {

 private Set<Address> addresses;

 @OneToMany(mappedBy = "person")
 public Set<Address> getAddresses() {
  return addresses;
 }

 public void setAddresses(Set<Address> addresses) {
  this.addresses = addresses;
 }
}


package com.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "Address")
public class Address {

 private Person person;

 @ManyToOne
 @JoinColumn(name = "person_id")
 public Person getPerson() {
  return person;
 }

 public void setPerson(Person person) {
  this.person = person;
 }
}


1) CascadeType None
If CascadeType is not given as shown above then we need to explicitly save a parent as well as all the childrens in to the session as shown below
Person person = new Person();
Address address1 = new Address();
session.save(person);
session.save(address1);




2) CascadeType.ALL : With this cascade type when a Parent is saved, updated, deleted Child will also gets saved, updated, deleted. No need to explicitly save or update or delete the childrens into the session.
private Set<Address> addresses;

@OneToMany(mappedBy = "person")
@Cascade( { CascadeType.ALL})
public Set<Address> getAddresses() {
 return addresses;
}

public void setAddresses(Set<Address> addresses) {
 this.addresses = addresses;
}
Person person = new Person();
Address address1 = new Address();
Address address2 = new Address();

person.setAddresses(new HashSet<Address>());
person.getAddresses().add(address1);
person.getAddresses().add(address2);

session.save(person);


3. CascadeType.SAVE_UPDATE: With this cascade type when a Parent is saved, updated Child will also gets saved, updated. No need to explicitly save or update the childrens into the session. In this delete of children will not work. Deletion need to be done explicitly

4) CascadeType.DELETE: This cascade type applies when delete operation performs against the database. In this cascade type, the child will get deleted when a Parent gets deleted from a session. No need to explicitly delete a child from the table.
List<Person> persons = session.createQuery(
  "from Person where" + " person_Id = 2").list();
for (Person person : persons) {
 session.delete(person);
}


5) CascadeType.DELETE_ORPHAN - Deprectaed in Hibernate 4.3.10

6) CascadeType.DETACH - With this cascade type when a Parent is detached Child will also gets detached.

7) CascadeType.EVICT - Deprectaed in Hibernate 4.3.10

8) CascadeType.MERGE - With this cascade type when a Parent is merged Child will also gets detached.

9) CascadeType.PERSIST : It means that save() or persist() operations cascade to Child entities.






1 comment: