C# – NHibernate parent mapping does not create child foreign-key

cnetnhibernate

I am just learning NHibernate. I have been using examples from the documentation and here at stackoverflow, but I must be missing something.

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.

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.

See the code sample below. Please advise.

Class Files

public class PARENT {
   private readonly IList<CHILD> _children = new List<CHILD>();
   public virtual Id { get; set; }
   public virtual void AddChild(CHILD child) {
      _children.add(child);
   }
}
public class CHILD {
   public virtual Id { get; set; }
}

Mappings

<class name="PARENT" table="Parent">
  <cache usage="read-write"/>

  <id name="Id" column="Id" unsaved-value="0" >
    <generator class="identity" />
  </id>

  <bag name="Children" access="field.camelcase-underscore" cascade="all-delete-orphan">
    <key column="ParentId"/>
    <one-to-many class="CHILD"/>
  </bag>

</class>
<class name="CHILD" table="Child">
  <cache usage="read-write"/>

  <id name="Id" column="Id" unsaved-value="0" >
    <generator class="identity"/>
  </id>

</class>

Best Answer

You must define both sides of the relationship in your mapping and object model. You then declare one as inverse="true" in the mapping. So something like this should work:

Class files

public class PARENT {
   private readonly IList<CHILD> _children = new List<CHILD>();
   public virtual Id { get; set; }
   public virtual void AddChild(CHILD child) {
      _children.add(child);
   }
}
public class CHILD {
   public virtual PARENT Parent { get; set; }
   public virtual Id { get; set; }
}

Mappings

<class name="PARENT" table="Parent">
  <cache usage="read-write"/>

  <id name="Id" column="Id" unsaved-value="0" >
    <generator class="identity" />
  </id>

  <bag name="Children" access="field.camelcase-underscore"
     cascade="all-delete-orphan" inverse="true">
    <key column="ParentId"/>
    <one-to-many class="CHILD"/>
  </bag>

</class>
<class name="CHILD" table="Child">
  <cache usage="read-write"/>

  <id name="Id" column="Id" unsaved-value="0" >
    <generator class="identity"/>
  </id>
  <many-to-one name="Parent" class="PARENT" column="ParentId" />

</class>

And you might want to change your AddChild method to this:

public virtual void AddChild(CHILD child) {
   _children.add(child);
   child.Parent = this;
}
Related Topic