I usually annotate my one-to-many
, many-to-many
and many-to-one
relationships with CascadeType.ALL
, because they provide me with all features I need. But I feel it can lead me to some problems in the future. Is it okay to use CascadeType.ALL
? What should I be aware of and why should I not use that?
-
Possible duplicate of [What is the meaning of the CascadeType.ALL for a @ManyToOne JPA association](https://stackoverflow.com/questions/13027214/what-is-the-meaning-of-the-cascadetype-all-for-a-manytoone-jpa-association) – Jonathan JOhx Sep 24 '19 at 05:44
4 Answers
Cascading is about persistence actions involving one object propagating to other objects via an association.
Cascading can apply to a variety of Hibernate actions, and it is typically transitive. The "cascade=CascadeType..." attribute of the annotation that defines the association says what actions should cascade for that association.
Cascade = "all" means to apply all primary cascade types.
As of Hibernate 5.3, these types are:
- "delete" / "remove",
- "detach" / "evict",
- "merge",
- "lock",
- "persist",
- "refresh",
- "replicate",
- "save_update" / "update"
(Some of those cascade type names are old and/or deprecated.)
There are three more compound types:
- "all_delete_orphan" - means the same as "all" plus enabling the deletion of entities that are orphaned by the cascading.
- "delete_orphan" - means "delete" plus orphan deletion.
- "none" - means no cascading.

- 723
- 7
- 19
-
You are talking about `@Cascade` annotation provided by Hibernate. Are you sure when I use cascade in JPA annotations it applies all of Hibernate cascading types? – Sep 23 '19 at 13:10
-
JPA doesn't have all of these cascade types. These are the types provided by JPA. cascade-all, cascade-persist, cascade-merge, cascade-remove, cascade-refresh, or cascade-detach – yashjain12yj Sep 23 '19 at 13:17
What does CascadeType.ALL
mean and when should I use?
CascadeType.ALL
are all operations (PERSIST, MERGE, REMOVE, REFRESH, DETACH)
that are propagated to the associated entity to the relating entities in persistence context.
So, this question is very general, if you know these definitions, you can identify if it is necessary or not to use CascadeType.ALL
.
DOCUMENTATION:
The EntityManager
has APIs to persist new entities, remove
(delete) existing entities, refresh
entity state from the datastore, and merge
detached entity state back into the persistence context.
When the EntityManager
is performing the above operations, you can instruct it to automatically cascade the operation to the entities held in a persistent field with the cascade property of your metadata annotation. This process is recursive. The cascade property accepts an array of CascadeType enum values.
CascadeType.PERSIST: When persisting an entity, also persist the entities held in this field. We suggest liberal application of this cascade rule, because if the EntityManager finds a field that references a new entity during flush, and the field does not use CascadeType.PERSIST, it is an error.
CascadeType.MERGE: When merging entity state, also merge the entities held in this field.
CascadeType.REMOVE: When deleting an entity, also delete the entities held in this field.
CascadeType.REFRESH: When refreshing an entity, also refresh the entities held in this field.
CascadeType.DETACH: When detaching an entity, also detach the entities held in this field.
REFERENCES:

- 5,784
- 2
- 17
- 33
This is something which you need to understand. first of all how effective you are designing your database also is your table independent or there is any relation with another table. please have a look on below points:
- If Your table has multiple link with another table and persisting data in a single table should cascade data to all dependent table then in that scenario you have to use CascadeType.ALL For Example:
EmployeeEntity.java
@Entity
@Table(name = "Employee")
public class EmployeeEntity implements Serializable
{
private static final long serialVersionUID = -1798070786993154676L;
@Id
@Column(name = "ID", unique = true, nullable = false)
private Integer employeeId;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstName;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastName;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="EMPLOYEE_ID")
private Set<AccountEntity> accounts;
//Getters and Setters Ommited
}
AccountEntity.java
@Entity
@Table(name = "Account")
public class AccountEntity implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Integer accountId;
@Column(name = "ACC_NO", unique = false, nullable = false, length = 100)
private String accountNumber;
@OneToOne (mappedBy="accounts", fetch = FetchType.LAZY)
private EmployeeEntity employee;
}
Look at the above source code for EmployeeEntity.java. It defines “cascade=CascadeType.ALL” and it essentially means that any change happened on EmployeeEntity must cascade to AccountEntity as well. If you save an employee, then all associated accounts will also be saved into database. If you delete an Employee then all accounts associated with that Employee also be deleted. Simple enough.
But what if we only want to cascade only save operations but not delete operation. Then we need to clearly specify it using below code.
@OneToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name="EMPLOYEE_ID")
private Set<AccountEntity> accounts;
There is no default cascade type in JPA. By default no operations are cascaded.

- 834
- 6
- 14
Apart from opinions above, would also like to add following points. From OOPs perspective, we should think in terms of Composition vs Aggregation. In java there is no away we can enforce strict relationship between parent and child unlike in DB using FK keys.
Conceptually CASCADE option in hibernate enforces us to maintain this strict relationship.

- 101
- 1
- 3