Hibernate: cascade = ALL includes REMOVE, which deletes children

Convenient on a true parent-child relationship, dangerous on a shared reference.

Code
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<OrderItem> items = new ArrayList<>();

// Correct - items only exist inside an order.

@ManyToOne(cascade = CascadeType.ALL)   // WRONG
private Customer customer;              // deleting an order deletes the customer
Output
orphanRemoval = true: removing an item from the list deletes the row.
cascade on a @ManyToOne to a shared entity is nearly always a bug.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11