CascadeType.ALL in JPA means that every entity state operation performed on a parent entity is also applied to its associated entities. It is shorthand for PERSIST, MERGE, REMOVE, REFRESH and DETACH. It is meant for relationships where the child cannot exist without the parent, such as an order and its order lines.
Cascading is configured on the relationship annotation (@OneToMany, @OneToOne, @ManyToOne, @ManyToMany) through the cascade attribute. In Jakarta Persistence 3.x and Hibernate 6 the types live in jakarta.persistence; older code uses javax.persistence with the same behaviour.
Using CascadeType.ALL is equivalent to listing all of these values:
| Cascade type | Effect on the children |
|---|---|
PERSIST | Children are persisted when the parent is persisted. |
MERGE | Children are merged when the parent is merged. |
REMOVE | Children are deleted when the parent is deleted. |
REFRESH | Children are reloaded when the parent is refreshed. |
DETACH | Children are detached when the parent is detached. |
Hibernate also has its own org.hibernate.annotations.CascadeType with provider-specific values, but with the standard JPA API the five values above are all you need.
Saving and deleting the parent now cascades:
Without cascading you would have to call persist on every line yourself, and deleting the order would fail with a foreign key error while lines still reference it.
orphanRemoval = true is related but different. It deletes a child when it is removed from the parent's collection or detached from the relationship, even if the parent itself is not deleted. CascadeType.REMOVE only acts when the parent is removed.
Together, cascade = CascadeType.ALL and orphanRemoval = true make the parent the full owner of the child lifecycle. This is the usual choice for composition. Do not use it for children that are shared with other parents.
Student and Course. Cascading REMOVE would delete a course when one student is removed. Use only PERSIST and MERGE, or none.Customer. Deleting the order must not delete the customer.A good rule: use CascadeType.ALL only on the owning side of a composition, where the child has no meaning without the parent.
ON DELETE CASCADE in the schema.FetchType.LAZY collections and then being surprised by extra queries when the provider loads children to cascade a remove.Yes. It covers PERSIST, MERGE, REMOVE, REFRESH and DETACH.
Not by itself. Removing a child from the parent collection only deletes the row if you also set orphanRemoval = true.
No. The default is no cascading at all, so you must opt in on each relationship.
Usually not, because REMOVE can delete entities that other parents still use.
CascadeType.ALL passes persist, merge, remove, refresh and detach from a parent entity to its related entities. Use it, ideally with orphanRemoval = true, for parent-owned children such as order lines, and avoid it on shared or many-to-many relationships where deleting one side must not delete the other.