Hibernate : What is the implecation of Cascade in Hibernate

The “Cascade” keyword is often appear on the collection mapping to manage the state of the collection automatically. Hibernate entities carries object graph or compisition in them. That means one object contents the object of several other object reference or entities (whose data is reqired to be saved in different table). Let’s say user object is required to be saved and it has orders object in it. If we apply cascade property then we each order is not reqired to be saved mannually, it would get automatically saved when user object is saved in session.

Sample code illustrating cascade property
//Stock.java
@OneToMany(mappedBy = “stock”)
@Cascade({CascadeType.ALL})
public Set getStockDailyRecords() {
return this.stockDailyRecords;
}

List of possible values of cascade property
1) cascade=”none”, the default, tells Hibernate to ignore the association.
2) cascade=”save-update” tells Hibernate to navigate the association when the
transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade=”delete” tells Hibernate to navigate the association and delete persistent
instances when an object is passed to delete().
4) cascade=”all” means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade=”all-delete-orphan” means the same as cascade=”all” but, in addition,
Hibernate deletes any persistent entity instance that has been removed
(dereferenced) from the association (for example, from a collection).
6) cascade=”delete-orphan” Hibernate will delete any persistent entity
instance that has been removed (dereferenced) from the association (for
example, from a collection).

One thought on “Hibernate : What is the implecation of Cascade in Hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s