Hibernate: When should I use Cascade.ALL and when should i specify them separately

Hibernate: When should I use Cascade.ALL and when should i specify them separately

cascade in hibernate
cascade detach
jpa cascade persist one to-many example

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?

Pro Hibernate 3, There is another type of cascading operation that has to do with deletes, named Rather than add all of these in a comma-separated list, Hibernate provides a Use the default cascading to specify how properties and collections (that do not  Introduction to Cascade in Hibernate Whenever the propagation takes place from one object to another in the hibernate framework or associated technology, persistence actions are involved in it. It is all about what persistence actions should

What does CascadeType.ALL mean and when should I use?

CascadeType.ALLare 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.


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.


EJB3.0 Specification - 2.1.7 Entity Relationships

Apache-openjpa docs JPA overview meta cascade

Beginning Hibernate, We do this by setting the cascade operations for the properties and fields of the lock • refresh These values can be concatenated in a comma-separated list to allow value named all that tells Hibernate to cascade all of these operations from the file, set the cascade attribute to the type (or types) you would like to use. Use of the cascade annotation element may be used to propagate the effect of an operation to associated entities. The cascade functionality is most typically used in parent-child relationships. If X is a managed entity, the remove operation causes it to become removed.

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:

  1. 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:


@Table(name = "Employee")
public class EmployeeEntity implements Serializable
    private static final long serialVersionUID = -1798070786993154676L;
    @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)
    private Set<AccountEntity> accounts;

    //Getters and Setters Ommited


@Table(name = "Account")
public class AccountEntity implements Serializable
    private static final long serialVersionUID = 1L;
    @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)
private Set<AccountEntity> accounts;

There is no default cascade type in JPA. By default no operations are cascaded.

Beginning Hibernate: From Novice to Professional, We do this by setting the cascade operations for the properties and fields ofthe will perform correctly if at least the save cascade operation is set for the Email entity's When all operations should be cascaded, Hibernate provides a shortcut you can tell Hibernate to use one of these cascading types for a relationship  I will consider XML formatted file hibernate.cfg.xml to specify required Hibernate properties in my examples. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application's classpath. Hibernate Properties

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.

Cascading, If you do not use cascade you would have to save both objects independently. <set name="teams" table="tteam" inverse="false" cascade="all"> <key Java Persistence and Hibernate provides both a way to configure cascading. If you configure orphan removal it will be deleted just by fact that it was removed from the  Using hibernate can we configure two schemas in hibernate.cfg.xml file. I dont want to use JPA. If configurtaion is achieved then, how to query data from each schema separately. Can u post a sample code. – shreekanth Jan 3 '13 at 6:54

Hibernate, its referenced 'StockDailyRecord' into database, you need to save both individually. You need to loop all the 'stockDailyRecords' and delete it one by one. Stock.hbm.xml --> <set name="stockDailyRecords" cascade="delete" a price, if you do not use it wisely (update or delete), it will generate many  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more @UniqueConstraint and @Column(unique = true) in hibernate annotation

Cascade in Hibernate | Examples, In both the methods if cascade property is set to all then it means that all the then we will delete both of them individually when we are not using cascading. Hibernate writes the values bound to the JDBC parameters and extracted from the JDBC results to this log category.This category should be used together with org.hibernate.SQL to also log the SQL statements. org.hibernate.pretty: Hibernate logs the state at flush time of max. 20 entities to this log category. org.hibernate.cache

The best way to map a @OneToOne relationship with JPA and , For the following examples, I'm going to use the following Post and PostDetails classes: @OneToOne (mappedBy = "post" , cascade = CascadeType.ALL, However, there can be only one post_details row associated with a post , so it Hibernate fetches the child entity as well, so, instead of only one  Introduction There are many ways you can map a one-to-one relationship with Hibernate. In this post, I’m going to demonstrate which mapping is the most efficient one from a database perspective. Domain Model For the following examples, I’m going to use the following Post and PostDetails classes: The Post entity is the parent, while the PostDetails is the child association because the