Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is cascading in Hibernate? [duplicate]

What is cascading in Hibernate ? There is a cascade attribute I have seen in the map tag. What is it meant for?

Like what does cascade = all mean? There are other attributes I read like
cascade="none|save-update|delete|all-delete-orphan|delete-orphan".

like image 626
saplingPro Avatar asked Jun 13 '13 12:06

saplingPro


People also ask

What is hibernate cascading?

Tags:cascade | hibernate. Cascade is a convenient feature to save the lines of code needed to manage the state of the other side manually. The “Cascade” keyword is often appear on the collection mapping to manage the state of the collection automatically.

How many types of cascade are there in hibernate?

Hibernate supports three additional Cascade Types along with those specified by JPA.

What is the purpose of Cascade property in Java hibernate?

Cascade is the feature provided by hibernate to automatically manage the state of mapped entity whenever the state of its relationship owner entity is affected.

What is cascade type merge?

CascadeType. MERGE : cascade type merge means that related entities are merged when the owning entity is merged.


2 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=..." 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.
like image 165
Stephen C Avatar answered Sep 16 '22 11:09

Stephen C


Cascading is Hibernate's way of using transitive persistence model. Transitive persistence is a technique that allows you to propagate persistence to transient (object not yet saved in database) and detached sub-graphs (child objects) automatically. For example, a newly created child object of already persistent parent object should automatically become persistent without a call to save() or persist() methods.

Cascading in Hibernate has many options like save-update, persist, merge, delete etc. Cascade='all' is a way to apply all cascading options.

like image 20
Prashant_M Avatar answered Sep 18 '22 11:09

Prashant_M