CascadeType is the enum in jakarta.persistence (formerly javax.persistence) that lists which EntityManager operations propagate from an entity to its associated entities. It has six values: PERSIST, MERGE, REMOVE, REFRESH, DETACH and ALL. You pass them to the cascade attribute of @OneToOne, @OneToMany, @ManyToOne or @ManyToMany.
Choosing the right values decides whether saving, updating or deleting a parent also saves, updates or deletes its children, so each constant is worth understanding on its own.
You never call CascadeType directly. It is an argument to a relationship annotation:
If you omit cascade, the default is an empty array and nothing propagates. The array form lets you combine several constants, and each one affects only its matching operation.
PERSIST propagates EntityManager.persist(). When you persist a new Author, every Book in its collection is persisted in the same call, as long as those books are new. If a book is already detached, persist throws PersistenceException (typically "detached entity passed to persist").
MERGE propagates EntityManager.merge(). It is the constant you need for updates that arrive as detached objects, for example a deserialised request body:
Always use the return value of merge, which is the managed copy. The object you passed in remains detached.
REMOVE propagates remove(). Removing an Author also schedules deletion of its books. Use it only for children owned by a single parent.REFRESH propagates refresh(). Refreshing an author reloads the author and its cascaded associations from the database, discarding unflushed changes.DETACH propagates detach(). Detaching the author also removes the cascaded books from the persistence context, which is useful before long-lived processing or when you want to stop dirty checking.REMOVE does not delete a child you merely remove from a collection. For that, add orphanRemoval = true on @OneToMany or @OneToOne.
ALL is shorthand for PERSIST, MERGE, REMOVE, REFRESH and DETACH:
This is the right mapping for composition, where the child cannot exist without the parent, such as order lines. It is the wrong mapping when entities are shared. The specification defines ALL as the union of those five operations.
| Relationship | Recommended cascade | Avoid |
|---|---|---|
Parent to owned children (@OneToMany) | ALL with orphanRemoval, or PERSIST and MERGE | None, if children are owned |
@OneToOne owned part | ALL | Cascading from the child back to the parent |
@ManyToOne | PERSIST, MERGE at most, usually none | REMOVE, ALL |
@ManyToMany | PERSIST, MERGE | REMOVE, ALL |
On a @ManyToMany between students and courses, REMOVE would delete courses still attended by other students, or fail with a foreign key violation.
Hibernate has an extra org.hibernate.annotations.CascadeType with constants such as LOCK and REPLICATE, applied with the Hibernate @Cascade annotation. Older Hibernate code used SAVE_UPDATE, which belongs to the native Session API rather than JPA; prefer the standard constants so your mappings stay portable.
CascadeType.ALL on @ManyToOne, which can delete a shared parent when one child goes.PERSIST and then seeing "object references an unsaved transient instance" when the transaction flushes.REMOVE to delete orphaned children; that needs orphanRemoval.add and remove helper methods.Six: ALL, PERSIST, MERGE, REMOVE, REFRESH and DETACH.
There is no default cascade. Without a cascade attribute, no operation propagates.
No. REMOVE is performed by the persistence provider with individual DELETE statements. ON DELETE CASCADE is a database foreign key rule that the provider does not see.
save() calls persist for new entities and merge for existing ones, so PERSIST and MERGE are what the call needs.
CascadeType selects which of persist, merge, remove, refresh and detach flow from one entity to another, with ALL combining them. Use ALL and orphanRemoval for owned children, restrict shared associations to PERSIST and MERGE, and never cascade REMOVE along @ManyToOne or @ManyToMany.