Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate parent mapping does not create child foreign-key
    primarykey
    data
    text
    <p>I am just learning NHibernate. I have been using examples from the documentation and here at stackoverflow, but I must be missing something. </p> <p>I have a PARENT object that has a collection of CHILD. A CHILD is meaningless without a PARENT, so the database has FK set to NOT NULL. Implementing NHibernate from CHILD to PARENT works fine, though I have no need for this direction of relationship.</p> <p>Instead, I tried to implement the PARENT owning the relationship, but I consistently get a Database error bubbling up "Cannot insert the value NULL into PARENT_ID". Neither the ID nor the entity of the PARENT is being stored in the CHILD when saving.</p> <p>See the code sample below. Please advise.</p> <p><strong>Class Files</strong></p> <pre><code>public class PARENT { private readonly IList&lt;CHILD&gt; _children = new List&lt;CHILD&gt;(); public virtual Id { get; set; } public virtual void AddChild(CHILD child) { _children.add(child); } } public class CHILD { public virtual Id { get; set; } } </code></pre> <p><strong>Mappings</strong></p> <pre><code>&lt;class name="PARENT" table="Parent"&gt; &lt;cache usage="read-write"/&gt; &lt;id name="Id" column="Id" unsaved-value="0" &gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;bag name="Children" access="field.camelcase-underscore" cascade="all-delete-orphan"&gt; &lt;key column="ParentId"/&gt; &lt;one-to-many class="CHILD"/&gt; &lt;/bag&gt; &lt;/class&gt; &lt;class name="CHILD" table="Child"&gt; &lt;cache usage="read-write"/&gt; &lt;id name="Id" column="Id" unsaved-value="0" &gt; &lt;generator class="identity"/&gt; &lt;/id&gt; &lt;/class&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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