Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question is wrong in and of itself, which is where all the confusion stems from. Arthur did a good job with his answer but it's clear from the comments the the confusion still remains so let me take a stab at it here.</p> <blockquote> <p>Do cascades hold only when I specify them on the entity owning the relationship?</p> </blockquote> <p>"cascade" is an attribute you specify on one (or possibly both in case of bi-directional) end of a relationship. It determines what <strong>actions</strong> performed on <strong>that</strong> end will be propagated to the <strong>other</strong> end. There are <a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-association-cascade" rel="noreferrer">many different types</a> of those actions defined in JPA and <a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-hibspec-cascade" rel="noreferrer">even more</a> defined in Hibernate extensions. This distinction is important - you should only talk about <strong>specific</strong> behavior being propagated and not "cascade" in general.</p> <p>PERSIST, MERGE, REFRESH propagate normally (from the end they were declared on to the other).</p> <p>REMOVE, however, is tricky because it can mean two different things. If you have a relationship between <strong>A</strong> and <strong>B</strong> and you're trying to remove <strong>A</strong>, you can either remove <strong>B</strong> on the other end OR you can remove the <strong>association</strong> but leave <strong>B</strong> intact. Hibernate makes a clear distinction between the two - you can declare REMOVE (DELETE) and <code>DELETE_ORPHAN</code> cascade types separately; JPA spec does not. Note that <code>DELETE_ORPHAN</code> is not supported to single-valued relationships (OneToOne / ManyToOne).</p> <p>Thus, propagation of REMOVE (by itself or when it's part of ALL) depends on whether relationship has a clear owner (uni-directional always does; bi-directional does if it's mapped using <strong>mappedBy</strong> and does not if it's mapped via <strong>join table</strong>) in which case it's propagated from owner to owned OR no owner in which case it's propagated in either direction but without <code>DELETE_ORPHAN</code> semantics unless it was explicitly specified. Typical example of the latter is bi-directional many-to-many.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload