The cascade attribute on a JPA relationship annotation (@OneToOne, @OneToMany, @ManyToOne, @ManyToMany) tells the persistence provider to apply an entity manager operation on the parent to the associated entities as well. Without it, JPA applies nothing by default: you must persist, merge or remove each related entity yourself.
Cascading is how you model an aggregate, such as an Order that owns its OrderLine rows, so that saving or deleting the root takes its children with it. It is also easy to misuse, so the rules below matter as much as the syntax.
Every relationship annotation accepts cascade, an array of CascadeType values. When you call an EntityManager operation on the owning entity, the provider repeats that operation on each associated entity whose cascade list includes the matching type.
With this mapping, entityManager.persist(customer) also persists every Order in the list, and entityManager.remove(customer) deletes the orders too. The default is no cascading at all, so a mapping with no cascade element behaves as if you had written cascade = {}.
Since Jakarta Persistence 3.0 the package is jakarta.persistence. Older projects on Java EE or Spring Boot 2 use javax.persistence; the attribute works the same way.
The CascadeType enum has six constants. Each one maps to an EntityManager method.
| CascadeType | Propagates | Typical use |
|---|---|---|
PERSIST | persist() | Save new children together with a new parent |
MERGE | merge() | Copy changes from a detached graph into the persistence context |
REMOVE | remove() | Delete children when the parent is deleted |
REFRESH | refresh() | Reload the whole graph from the database |
DETACH | detach() | Detach the whole graph from the persistence context |
ALL | All of the above | Child fully owned by the parent |
You can combine values in an array:
Hibernate also offers its own org.hibernate.annotations.CascadeType, for example for LOCK or REPLICATE. Use the standard JPA values unless you need a provider-specific operation.
With PERSIST, the new children are saved when the parent is. Without it, persisting a parent that references a transient child fails when the transaction flushes, with an IllegalStateException or a Hibernate TransientObjectException that says the object references an unsaved transient instance.
MERGE matters when you receive a detached graph, for example from a REST request, and call entityManager.merge(customer). The provider copies the state of the detached customer and of each cascaded order onto managed instances and returns the managed customer. Keep using the returned object, because the one you passed in stays detached.
REMOVE deletes children when you call remove() on the parent. It does not delete a child that you simply take out of the parent's collection. That is the job of orphanRemoval = true:
| Action | CascadeType.REMOVE | orphanRemoval = true |
|---|---|---|
em.remove(parent) | Children deleted | Children deleted |
parent.getOrders().remove(order) | Nothing happens to the row | Order row deleted at flush |
Set a @OneToOne child reference to null | Nothing | Child row deleted |
Cascade and orphan removal are different features that are usually combined for parent-owned children. Orphan removal is only valid on @OneToOne and @OneToMany.
Database-level ON DELETE CASCADE is separate again. It is executed by the database, so the persistence context is not told that the rows are gone. Hibernate can use it for schema generation through @OnDelete, but plain CascadeType.REMOVE issues one DELETE per child, which can be slow for large collections.
Cascade fits a parent-child relationship where the child has no life of its own. Follow these rules:
Order to OrderLine, not on every association.REMOVE or ALL on @ManyToOne. Deleting one order must not delete the shared customer.REMOVE and ALL on @ManyToMany. Deleting one student would delete courses shared with other students.PERSIST and MERGE when the related entities are shared or created elsewhere.addOrder.Cascade also interacts with fetch. Cascading REMOVE or MERGE to a lazy collection forces the provider to initialise it, which costs an extra query.
Spring Data repositories call persist for new entities and merge for existing ones, so the same cascade rules apply to save().
Note that save() decides between persist and merge by checking whether the entity is new, so a child with an assigned id may be treated as existing and merged. Check your id strategy if you see unexpected SELECT statements.
There is none. If you omit cascade, no operation propagates to the related entities and you must persist, merge and remove them explicitly.
REMOVE acts when you remove the parent. orphanRemoval = true also deletes a child that is removed from the parent's collection or disconnected from a one-to-one reference.
It is safe for a child that is fully owned by one parent. It is dangerous on shared associations such as @ManyToOne and @ManyToMany, where it can delete data other entities still use.
The parent references a child that is not persisted yet and the mapping has no PERSIST cascade. Either add CascadeType.PERSIST or save the child first.
The cascade attribute copies persist, merge, remove, refresh and detach from a parent entity to its associations. It is off by default. Use ALL together with orphanRemoval only for children that a single parent fully owns, use PERSIST and MERGE for lighter relationships, and never cascade REMOVE across shared references.