Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Straight from the <a href="http://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html#example-parentchild-cascades" rel="noreferrer">documentation</a>. This explains your problem exactly i believe:</p> <p>However, this code</p> <pre><code>Parent p = (Parent) session.Load(typeof(Parent), pid); // Get one child out of the set IEnumerator childEnumerator = p.Children.GetEnumerator(); childEnumerator.MoveNext(); Child c = (Child) childEnumerator.Current; p.Children.Remove(c); c.Parent = null; session.Flush(); </code></pre> <p>will not remove c from the database; it will only remove the link to p (and cause a NOT NULL constraint violation, in this case). You need to explicitly Delete() the Child.</p> <pre><code>Parent p = (Parent) session.Load(typeof(Parent), pid); // Get one child out of the set IEnumerator childEnumerator = p.Children.GetEnumerator(); childEnumerator.MoveNext(); Child c = (Child) childEnumerator.Current; p.Children.Remove(c); session.Delete(c); session.Flush(); </code></pre> <p>Now, in our case, a Child can't really exist without its parent. So if we remove a Child from the collection, we really do want it to be deleted. For this, we must use cascade="all-delete-orphan".</p> <pre><code>&lt;set name="Children" inverse="true" cascade="all-delete-orphan"&gt; &lt;key column="parent_id"/&gt; &lt;one-to-many class="Child"/&gt; &lt;/set&gt; </code></pre> <p>Edit: </p> <p>With regards to the inverse stuff, i believe this only determines how the sql is generated, see this <a href="http://simoes.org/docs/hibernate-2.1/155.html" rel="noreferrer">doc</a> for more info.</p> <p>One thing to note is, have you got </p> <pre><code>not-null="true" </code></pre> <p>on the many-to-one relationship in your hibernate config?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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