Hello, 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>