Hibernate Cascading Levels

In Simple terms, Cascading in Hibernate means the propagation of same action(for eg: save, delete, etc.) from Parent to Child level entity.

Depending on what type of action to perform there are different cascade types.

Let’s have a look at an example:

Employee.java Entity Class

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="EMPLOYEE_ID")
    private Set<Project> projects;
	...
}

Project.java Entity Class

@Entity
public class Project {
   @Id
   private Integer projectId;
   private String projectDescription;
   private String projectDuration;
  
   @OneToOne (mappedBy="projects",  fetch = FetchType.LAZY)
   private Employee employee;
   ...
}

cascade=CascadeType.ALL” essentially means that any change which happens on Employee Entity must cascade to Project Entity as well. If you save an employee, then all associated projects will also be saved into database. If you delete an Employee then all projects associated with that Employee will also be deleted.

But what if we only want to cascade only save operations but not delete operation. Then we need to clearly specify it using correct CascadeType, see the below code.

@OneToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name="EMPLOYEE_ID")
private Set<Project> projects;

Now only when save() or persist() methods are called using employee instance then only projects will be persisted. If any other method is called on session, it’s effect will not affect/cascade to projects.

JPA – Cascade Types

The cascade types supported by the Java Persistence Architecture are as below:

  1. CascadeType.PERSIST : cascade type presist means that save() or persist() operations cascade to related entities.
  2. CascadeType.MERGE : cascade type merge means that related entities are merged when the owning entity is merged.
  3. CascadeType.REFRESH : cascade type refresh does the same thing for the refresh() operation.
  4. CascadeType.REMOVE : cascade type remove removes all related entities association with this setting when the owning entity is deleted.
  5. CascadeType.DETACH : cascade type detach detaches all related entities if a “manual detach” occurs.
  6. CascadeType.ALL : cascade type all is shorthand for all of the above cascade operations.

There is no default cascade type in JPA. By default no operations are cascaded.

Hibernate – Cascade Types

Now lets understand what is cascade in hibernate in which scenario we use it.

Apart from JPA provided cascade types, there is one more cascading operation in hibernate which is not part of the normal set above discussed, called “orphan removal“. This removes an owned object from the database when it’s removed from its owning relationship.

Let’s understand with an example. In our Employee and Project entity example, I have updated them as below and have mentioned “orphanRemoval = true” on projects. It essentially means that whenever I will remove a ‘ project from projects set’ (which means I am removing the relationship between that project and Employee); the project entity which is not associated with any other Employee on database (i.e. orphan) should also be deleted.

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @OneToMany(orphanRemoval = true, mappedBy = "employee")
    private Set<Project> projects;
	...
}

Summary

In this tutorial, we have seen different ways to use cascade type operations in JPA hibernate.
I hope you liked it !


Leave a Comment