开发者

nhibernate - problem with merging collections

开发者 https://www.devze.com 2023-01-03 22:57 出处:网络
I have parent/child relationship, and child have composite id Parent <id name=\"Id\" type=\"Int32\">

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"/>
&l开发者_运维知识库t;/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.


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/>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号