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

user12099587 :

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?

yashjain12yj :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308496&siteId=1