What is Cascading in JPA?
Cascading in JPA is a feature that automatically applies database operations performed on a parent entity to its related child entities.
In simple words:
βWhen an action is performed on a parent entity, the same action is automatically applied to related entities.β
Why Cascading is Needed
Enterprise applications often contain relationships such as:
- Customer β Orders
- Student β Courses
- User β Roles
Without cascading:
- Developers must save child entities manually
- Delete operations become complex
- More boilerplate code is required
Cascading simplifies these operations.
Simple Real-Time Example
Suppose:
- One customer has multiple orders
When Customer is saved:
- Orders should also be saved automatically
This is achieved using cascading.
Basic Cascading Example
Customer Entity
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(
mappedBy = "customer",
cascade = CascadeType.ALL
)
private List<Order> orders;
}
Order Entity
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String product;
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
}
What Happens Internally?
When:
customerRepository.save(customer);
is executed:
- Customer is saved
- Orders are also saved automatically
Generated SQL Example
INSERT INTO customers(name)
VALUES ('Naresh');
INSERT INTO orders(product, customer_id)
VALUES ('Laptop', 1);
INSERT INTO orders(product, customer_id)
VALUES ('Mobile', 1);
What is CascadeType?
JPA provides different cascade operations using:
CascadeType
Main Cascade Types in JPA
| Cascade Type | Description |
|---|---|
| PERSIST | Save child entities automatically |
| MERGE | Update child entities automatically |
| REMOVE | Delete child entities automatically |
| REFRESH | Refresh child entities automatically |
| DETACH | Detach child entities automatically |
| ALL | Apply all cascade operations |
1. CascadeType.PERSIST
Automatically saves child entities when parent entity is saved.
PERSIST Example
@OneToMany(
cascade = CascadeType.PERSIST
)
How PERSIST Works
Saving Customer also saves Orders automatically.
2. CascadeType.MERGE
Automatically updates child entities when parent entity is updated.
MERGE Example
@OneToMany(
cascade = CascadeType.MERGE
)
How MERGE Works
Updating Customer also updates Orders automatically.
3. CascadeType.REMOVE
Automatically deletes child entities when parent entity is deleted.
REMOVE Example
@OneToMany(
cascade = CascadeType.REMOVE
)
How REMOVE Works
Deleting Customer also deletes Orders automatically.
Generated Delete Query Example
DELETE FROM orders
WHERE customer_id = 1;
DELETE FROM customers
WHERE id = 1;
4. CascadeType.REFRESH
Refreshes child entities from the database when parent entity is refreshed.
REFRESH Example
@OneToMany(
cascade = CascadeType.REFRESH
)
5. CascadeType.DETACH
Detaches child entities from persistence context when parent entity is detached.
DETACH Example
@OneToMany(
cascade = CascadeType.DETACH
)
6. CascadeType.ALL
Applies all cascade operations automatically.
ALL Example
@OneToMany(
cascade = CascadeType.ALL
)
What CascadeType.ALL Includes
- PERSIST
- MERGE
- REMOVE
- REFRESH
- DETACH
How Cascading Works with Relationships
Cascading is commonly used with:
- @OneToOne
- @OneToMany
- @ManyToOne
- @ManyToMany
@OneToOne Cascading Example
@OneToOne(
cascade = CascadeType.ALL
)
private Profile profile;
@ManyToMany Cascading Example
@ManyToMany(
cascade = CascadeType.PERSIST
)
private List<Role> roles;
What is Orphan Removal?
Orphan removal automatically deletes child entities removed from collections.
Orphan Removal Example
@OneToMany(
orphanRemoval = true
)
How Orphan Removal Works
If an Order is removed from:
customer.getOrders()
Hibernate automatically deletes it from the database.
Advantages of Cascading
- Reduces boilerplate code
- Simplifies entity management
- Automatic child operations
- Improves developer productivity
- Better relationship handling
Disadvantages of Improper Cascading
- Accidental data deletion
- Performance issues
- Unexpected database operations
- Complex debugging
Real-Time Example in E-Commerce Application
E-commerce application contains:
- Customer
- Orders
- Payments
When Customer is deleted:
- Orders may also need deletion
Cascading handles this automatically.
Best Practices for Cascading
- Use CascadeType.ALL carefully
- Avoid REMOVE on shared entities
- Use PERSIST and MERGE selectively
- Monitor generated SQL queries
- Use orphanRemoval when appropriate
When NOT to Use Cascading
- Shared child entities
- Large many-to-many relationships
- Critical financial data
Difference Between Cascading and Fetch Type
| Feature | Cascading | Fetch Type |
|---|---|---|
| Purpose | Controls operations | Controls data loading |
| Affects | Save, update, delete | Data retrieval |
| Examples | PERSIST, REMOVE | EAGER, LAZY |
Common Interview Questions on Cascading
What is cascading in JPA?
Cascading automatically applies operations from parent entity to child entities.
What is CascadeType.ALL?
It applies all cascade operations automatically.
What is CascadeType.PERSIST?
It automatically saves child entities.
What is orphanRemoval?
It automatically deletes removed child entities.
Why should REMOVE be used carefully?
Because it may accidentally delete important child data.
Conclusion
Cascading is one of the most important features in JPA and Hibernate relationship management.
It helps developers:
- Automate child entity operations
- Reduce boilerplate code
- Simplify relationship handling
- Improve application maintainability
Understanding cascading is essential for Spring Boot developers because enterprise applications heavily rely on entity relationships and automatic database operations.
Proper use of cascading improves:
- Performance
- Maintainability
- Scalability
- Database consistency