Search This Blog

Tuesday, November 13, 2018

JPA Cascade Types

The cascade types supported by the Java Persistence Architecture are as below:
  1. CascadeType.PERSIST : means that save() or persist() operations cascade to related entities.
  2. CascadeType.MERGE : means that related entities are merged into managed state when the owning entity is merged.
  3. CascadeType.REFRESH : does the same thing for the refresh() operation.
  4. CascadeType.REMOVE : removes all related entities association with this setting when the owning entity is deleted.
  5. CascadeType.DETACH : detaches all related entities if a “manual detach” occurs.
  6. CascadeType.ALL : is shorthand for all of the above cascade operations.
The cascade configuration option accepts an array of CascadeTypes; thus, to include only refreshes and merges in the cascade operation for a One-to-Many relationship in the following example, you might see the following:

@OneToMany(cascade={CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.LAZY)
@JoinColumn(name="ACCOUNT_ID")
private Set<AccountEntity> accounts;

Above cascading will cause accounts collection to be only merged and refreshed.