The CascadeType enumeration in JPA defines which entity operations are passed on from one entity to the entities it references. When you set cascade on a relationship annotation such as @OneToMany, the persistence provider repeats the chosen operation on the related entities, so you do not have to call it on each one manually.
The enum is jakarta.persistence.CascadeType (it was javax.persistence.CascadeType before Jakarta EE 9 and Spring Boot 3). By default a relationship has no cascading at all.
| Value | Operation cascaded | Entity manager call |
|---|---|---|
PERSIST | Make a new entity managed | persist() |
MERGE | Copy state of a detached entity into a managed one | merge() |
REMOVE | Delete the entity | remove() |
REFRESH | Reload state from the database | refresh() |
DETACH | Remove from the persistence context | detach() |
ALL | All of the above | all of the above |
You can pass one value or an array: cascade = {CascadeType.PERSIST, CascadeType.MERGE}.
PERSIST saves new related entities when the parent is persisted. Without it, persisting a parent that points to a transient child fails at flush time with an error such as TransientPropertyValueException in Hibernate.
MERGE copies the state of a detached graph into the persistence context. It is what makes merge(author) update the author and its books together, for example when an object arrives from a web request and is saved in a new transaction.
REMOVE deletes the related entities when the parent is deleted. Use it only when the child cannot live without the parent, such as order lines. Do not use it on shared relationships: removing one Student should not delete the Course that other students attend.
REFRESH reloads the parent and its children from the database, discarding in-memory changes. It is useful after a database trigger or another process changed the rows.
DETACH removes the parent and the related entities from the persistence context, so they are no longer tracked and changes are not flushed. It is often used for large batch jobs to free memory.
ALL combines every value above. It is the usual setting for a parent that fully owns its children, often together with orphanRemoval = true:
Cascade settings apply along each relationship separately. If Library cascades PERSIST to Author, and Author cascades PERSIST to Book, persisting a library persists its authors and their books. If the second link has no cascade, the books stay transient and the flush fails. Check every hop of the object graph, and test with a real database or an in-memory one such as H2 so that the failure shows up before production.
Here removing a library will not remove its authors, because REMOVE is not listed. That is deliberate when authors can belong to several libraries.
ALL with orphanRemoval = true.PERSIST and MERGE at most, never REMOVE.@OneToMany or @OneToOne parent side, not on @ManyToOne.JPA cascade is applied by the provider in memory and is not the same as a database ON DELETE CASCADE constraint. Hibernate also offers its own @org.hibernate.annotations.Cascade for provider-specific behaviour, but the JPA values cover most needs.
There is none. If you do not set cascade, no operation is propagated.
REMOVE deletes children when the parent is deleted. orphanRemoval = true also deletes a child when it is removed from the parent's collection.
save() use PERSIST or MERGE?save() calls persist for new entities and merge for existing ones, so cascading of those two types matters.
Yes, pass an array such as {CascadeType.PERSIST, CascadeType.MERGE}.
CascadeType controls which of persist, merge, remove, refresh and detach pass from a parent to its related entities, and ALL combines them. Choose the smallest set that matches who owns the lifecycle of the children, and avoid REMOVE and ALL on shared associations.