Search

2.4.7. Cascade

download PDF
Hibernate offers more operations than the Java Persistence specification. You can use the @Cascade annotation to cascade the following operations:
  • PERSIST
  • MERGE
  • REMOVE
  • REFRESH
  • DELETE
  • SAVE_UPDATE
  • REPLICATE
  • DELETE_ORPHAN
  • LOCK
  • EVICT
This is especially useful for SAVE_UPDATE (which is the operation cascaded at flush time if you use plain Hibernate Annotations - Hibernate EntityManager cascade PERSIST at flush time as per the specification). DELETE_ORPHAN applies only to @OneToMany associations, and indicates that the delete()/remove() operation should be applied to any child object that is removed from the association. In other words, if a child is dereferenced by a persistent parent and if DELETE_ORPHAN is used, the "orphaned" child is deleted.
@OneToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE} ) @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
public Collection<Employer> getEmployers()
It is recommended to use @Cascade to compliment @*To*(cascade=...) as shown in the previous example.
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.