views:

43

answers:

1

I have parent/child relationship, and child have composite id

Parent

    <id name="Id" type="Int32">
        <generator class="identity" />
    </id>

<set name="Children" table="CTable" cascade="all-delete-orphan" inverse="true" lazy="false" >
  <key column="ParentId"/>
  <one-to-many class="ChildrenClass"/>
</set>

Child

<composite-id unsaved-value="none">
  <key-property name="ParentId"/>
  <key-property name="ChildId"/>
</composite-id>

What I want: 1) Get Parent from the DB, close the session, pass Parent from service to client 2) When Parent is back from client, save it and all its children in the DB, in a new session

In step 2, I call var merged = Session.Merge(product);

I use Merge() because it seems the only way to make NHib handle added/deleted elements in children collection.. BUT nHibernate reset all id-s in newly added children, so I have new children with ParentId == 0 and ChildId == 0. So the question is - how to tell NHibernate to keep ids from entity that is passed in Merge()?.. Please help.

A: 

Your mapping (and class model) is not correct.

Child should have a reference to Parent, not an Id. And the mapping should be a <key-many-to-one/>

Diego Mijelshon
Dmitry
"Obviously"? You are wrong. Bidirectional relationships are fully supported by NHibernate. I suggest that you read the docs first.
Diego Mijelshon
Diego, I managed to get it work, don't know why it didn't at the first time. Thanks!
Dmitry