30

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".

informatik01
  • 16,038
  • 10
  • 74
  • 104
saplingPro
  • 20,769
  • 53
  • 137
  • 195

2 Answers2

24

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.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
7

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.

Mayank Jain
  • 369
  • 5
  • 21
Prashant_M
  • 2,868
  • 1
  • 31
  • 24