Open In App

Hibernate - Different Cascade Types

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Hibernate, when we deal with entities connected through relationships like a Customer who has multiple Orders, sometimes we want to perform some operations like save, update, delete, and refresh on the parent to automatically apply to its child entities. This behavior is called cascading.

Cascading allows us to do specific operations as we discussed save, update, or delete, from a parent entity to its associated child entities. The benefit is that we do not need to write extra code to handle each related entity individually. Hibernate provides many cascade types, and each type controls which operations are applied. In this article, we will go through all of them one by one, with examples and explanations.

Different Cascade Types in Hibernate

Hibernate provides different types of cascade options that can be used to manage the relationships between entities. Below are the different cascade types in Hibernate:

  1. CascadeType.ALL
  2. CascadeType.PERSIST
  3. CascadeType.MERGE
  4. CascadeType.REMOVE
  5. CascadeType.REFRESH
  6. CascadeType.DETACH
  7. CascadeType.REPLICATE
  8. CascadeType.SAVE_UPDATE

These cascade types can be used individually or in combination to manage the relationships between entities based on the requirements of the application. It is very important to use cascade types carefully because they can cause unintended things if not used properly.

1. CascadeType.ALL

This type cascades all operations, persist (save), merge (update), remove (delete), refresh, and detach. When we apply CascadeType.ALL, any action on the parent will automatically apply to its child entities.

Example:

Java
@Entity
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
    private Set<Order> orders = new HashSet<>();

    // getters and setters
}

@Entity
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;

    // getters and setters
}

Explanation: If we save, update, or delete a Customer, all linked Orders will be saved, updated, or deleted automatically. This is useful when the child entities are fully dependent on the parent.


2. CascadeType.PERSIST

This cascade type only applies the persist (save) operations. When we save a parent entity, any new child entities will be saved automatically, but other operations like delete or update will not cascade.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.PERSIST)
private Set<Order> orders = new HashSet<>();

Explanation: When a new Customer is saved, any new Orders associated with it are saved too. Deleting the Customer will not delete the Orders.


3. CascadeType.MERGE

This type cascades merge (update) operations. When we update or merge a detached parent entity, the changes to its child entities will be merged automatically as well.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.MERGE)
private Set<Order> orders = new HashSet<>();

Explanation: If a detached Customer entity is merged into the current session, all its associated Order entities are also merged and updated accordingly.


4. CascadeType.REMOVE

This cascade type applies only remove (delete) operations. When the parent entity is deleted, all its child entities are also deleted.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.REMOVE)
private Set<Order> orders = new HashSet<>();

Explanation: Deleting a Customer will automatically delete all their related Orders. This prevents orphaned orders, but we should use it cautiously to avoid unintended deletions.


5. CascadeType.REFRESH

This cascade type applies only refresh operations. When the parent entity is refreshed from the database, the child entities are also refreshed.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.REFRESH)
private Set<Order> orders = new HashSet<>();

Explanation: Refreshing a Customer reloads its state from the database and also refreshes all associated Order entities.


6. CascadeType.DETACH

This cascade type only applies the detach operations. When the parent entity is detached from the persistence context, the child entities are detached too.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.DETACH)
private Set<Order> orders = new HashSet<>();

Explanation: If we detach a Customer from the session, its related Orders will also become detached.


7. CascadeType.REPLICATE

This cascade type only applies the replicate operations which is mostly used in specific Hibernate replication scenarios.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.REPLICATE)
private Set<Order> orders = new HashSet<>();

Explanation: When a Customer is replicated means copied to another session or database, its associated Orders are replicated as well.


8. CascadeType.SAVE_UPDATE

This cascade applies save or update operations, similar to combining persist and merge.

Example:

Java
@OneToMany(mappedBy = "customer", cascade = CascadeType.SAVE_UPDATE)
private Set<Order> orders = new HashSet<>();

Explanation: When a Customer is saved or updated, the Orders associated with it will also be saved or updated automatically.

Each cascade type specifies a different behavior for cascading operations from the parent entity to its child entities. By choosing the appropriate cascade type for a given relationship, you can simplify your code and reduce the amount.


Next Article
Article Tags :

Similar Reads