0
0
Spring Bootframework~5 mins

Cascade types and behavior in Spring Boot

Choose your learning style9 modes available
Introduction

Cascade types help manage related data automatically. They make sure changes in one object affect connected objects without extra code.

When saving a parent object and you want its child objects saved too.
When deleting a parent and you want all its related children deleted automatically.
When updating a parent and you want changes to reflect on its related children.
When detaching a parent from the database and you want to detach its children as well.
When refreshing a parent object and you want to refresh its related children.
Syntax
Spring Boot
@OneToMany(cascade = CascadeType.ALL)
private List<Child> children;

Use cascade attribute inside relationship annotations like @OneToMany, @ManyToOne, @OneToOne, or @ManyToMany.

CascadeType.ALL means all cascade operations apply: persist, merge, remove, refresh, detach.

Examples
This saves the orders automatically when the parent is saved.
Spring Boot
@OneToMany(cascade = CascadeType.PERSIST)
private List<Order> orders;
This updates and deletes the profile when the parent is updated or deleted.
Spring Boot
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.REMOVE})
private Profile profile;
This detaches tags from the persistence context when the parent is detached.
Spring Boot
@ManyToMany(cascade = CascadeType.DETACH)
private Set<Tag> tags;
Sample Program

This example shows a Parent entity with children. When the parent is saved, all children are saved automatically because of CascadeType.ALL. The output shows how many children were saved and retrieved.

Spring Boot
import jakarta.persistence.*;
import java.util.*;

@Entity
class Parent {
    @Id
    @GeneratedValue
    private Long id;

    public Long getId() {
        return id;
    }

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Child> children = new ArrayList<>();

    public Parent() {}

    public void addChild(Child child) {
        children.add(child);
    }

    public List<Child> getChildren() {
        return children;
    }
}

@Entity
class Child {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    public Child() {}

    public Child(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class CascadeDemo {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        Parent parent = new Parent();
        parent.addChild(new Child("Child 1"));
        parent.addChild(new Child("Child 2"));

        em.persist(parent); // saves parent and children because of cascade

        em.getTransaction().commit();

        em.getTransaction().begin();
        Parent foundParent = em.find(Parent.class, parent.getId());
        System.out.println("Children count: " + foundParent.getChildren().size());
        em.getTransaction().commit();

        em.close();
        emf.close();
    }
}
OutputSuccess
Important Notes

Be careful with CascadeType.REMOVE to avoid accidentally deleting related data.

Use orphanRemoval = true to delete child objects if they are removed from the parent's collection.

Cascade only works on entity relationships managed by JPA, not on unrelated objects.

Summary

Cascade types automate saving, updating, deleting related entities.

Use cascade to keep related data consistent without extra code.

Choose cascade types carefully to avoid unwanted data loss.