nhibernate - 合并集合的问题

发布于 2024-09-06 20:02:37 字数 850 浏览 1 评论 0原文

我有父/子关系,孩子有复合 id

父子

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

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

我想要的 1)从数据库获取Parent,关闭会话,将Parent从服务传递到客户端 2)当父级从客户端返回时,将其及其所有子级保存在数据库中的新会话中

在步骤2中,我调用 var merged = Session.Merge(product);

我使用 Merge() 因为它似乎是使 NHib 处理子集合中添加/删除元素的唯一方法。但是 nHibernate 重置了新添加的子集合中的所有 id-s,所以我有新的子集合 ParentId == 0 和 ChildId == 0. 所以问题是 - 如何告诉 NHibernate 保留 Merge() 中传递的实体的 id?...请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-09-13 20:02:37

您的映射(和类模型)不正确。

子项应该引用父项,而不是 ID。映射应该是

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文