views:

19

answers:

1

i have a collection in the mapping:

<bag name="Values" cascade="all-delete-orphan" lazy="false" inverse="true">
  <key column="[TemplateId]"/>
  <one-to-many class="MyNamespace.Value, MyLib"/>
</bag>

the Value object has a foreign key [TemplateId]. both entities has their generator set to "identity". when i call session.Save() for the parent Template object, the Value objects has their [TemplateId] (the foreign key) set to zero, so an SQL exception appears. how do i forse nhibernate to set the FK value for the child items to the value of the inserted parent object?

+1  A: 

i've managed it myself: the only thing i was needed to do is to design child object mapping and persistent the following way:

<many-to-one name="Template" class="MyNamespace.Template, MyLib"
                 column="[TemplateId]" not-null="true" />

so the child object has a reference to the parent instead of the parent's Id

npeBeg
the solution was taken from here: http://stackoverflow.com/questions/1917845/nhibernate-parent-mapping-does-not-create-child-foreign-key
npeBeg

related questions