views:

477

answers:

2

I am trying to save a record which has a many-to-one property mapping. I attempt to assign a newly created parent object here (or use an existing, but that works fine) but I get an error when it tries to add the ID of the parent object to the child's table. Saying it cannot add NULL to the table, which is true, but I thought nHibernate was clever enough to save the parent object first, even if I am adding the child.

Mappings:

Child:

 <many-to-one name="parent" class="ParentClass" column="parentID" cascade="all"/>

Parent:

<bag name="DataList" table="ChildTable" lazy="false" inverse="true" cascade="all-delete-orphan">
      <key column="parentID" />
      <one-to-many class="ChildClass" />
    </bag>

Basically, Is this something nHibernate can do?

+1  A: 

I can't see your parent mapping, but I'd wager if you have inverse = true and cascading turned on there for a set of child objects, and you allow nulls in the parentID column of your child object, when the parent got around to saving it would update the child object properly.

tom.dietrich
A: 

This behavious can be caused by your primary key being "assigned" instead of generated by Nhibernate.

Noctris