Cascade Annotations Flashcards
CascadeType.PERSIST
When the parent is persisted, the child entities are also persisted
@OneToMany(mappedBy = “parent”, cascade = CascadeType.PERSIST)
private List<Child> children;</Child>
CascadeType.MERGE
When the parent is merged (updated), the child entities are also merged.
CascadeType.REMOVE
When the parent is removed, the child entities are also removed.
CascadeType.REFRESH
When the parent is refreshed, the child entities are also refreshed to reflect the latest state from the database.
CascadeType.DETACH
When the parent is detached, the child entities are also detached.
CascadeType.ALL
Includes all of the above cascade types.
Example
@OneToMany(mappedBy = “parent”, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Child> children = new ArrayList<>();
When you save or delete a Parent entity, the associated Child entities will also be saved or deleted.</Child>