维护多个一对多

发布于 2024-11-28 22:37:43 字数 388 浏览 5 评论 0原文

NHibernate 一对一与 2 多对-one

是否有一种简单的方法来维护多个一对多关系,这些关系被用作伪一对一。

在此处输入图像描述

例如,

如果我有 2 个实体,用户和联系人,它们通过每个实体 (User. ContactId 和 Contact.UserID)。

保持每个参考点指向另一个参考点的最佳方法是什么?系统用不同的联系人更新用户是错误的,但联系人仍然引用用户...

Following on from NHibernate one-to-one vs 2 many-to-one

Is there an easy way to maintain multiple one-to-many relationships which are being used as a pseudo one-to-one.

enter image description here

E.g.

If I have 2 entities, User and Contact, which are related by a FK on each (User.ContactId and Contact.UserID).

What is the best way to maintain that each reference points at the other. It would be wrong for the system to update User with a different contact, but the Contact still references User...

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

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

发布评论

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

评论(1

2024-12-05 22:37:43

如果删除一个冗余外键,很可能根本不需要维护它。您的数据库架构不应允许类似的异常(userX 引用 contactX,但 contactX 引用 userY)。从概念上讲,用户和联系人之间存在一对一的关系。为什么 NHibernate 映射中不采用一对一的映射?如果这是因为延迟加载,则 可为空不支持 /strong> NHibernate 中的一对一?这个问题有一个解决方案,就是不涉及数据库中多余的外键。

1) 在用户映射中定义一个虚假列表。列表只能包含一项或零项。零被视为 NULL(无联系人)。

<bag 
  name="_contact" 
  table="UserContacts" 
  lazy="true" 
  inverse="true" 
  cascade="all-delete-orphan" >

    <key column="UserId" />
    <one-to-many class="Contact" />
</bag>

在联系人映射中定义一对一:

<one-to-one name="_user" class="User" constrained="true" />

在数据库中,您需要有 PK Users.Id 和一个(!)外键 Contacts.UserID。

2)另一种选择是在用户映射中简单地进行多对一和一个 FK Users.ContactId

<many-to-one 
   name="_contact" 
   column="ContactId" 
   cascade="all-delete-orphan" 
   unique="true" 
   lazy="proxy"/>

无论哪种方式,都不需要您询问的维护,并且不可能出现异常。

Most likely you don't need to maintain this at all if you remove one of redundant foreign keys. Your database schema should not allow anomalies like that (userX references contactX but contactX references userY). Conceptually you have one-to-one relationship between user and contact. Why not have one-to-one in NHibernate mappings? If this is because of lazy loading that is not supported for nullable one-to-one in NHibernate? There is a solution to this problem that does not involve redundant foreign keys in the database.

1) In User mapping define a bogus list. List can have only one or zero items. Zero is treated as NULL (no Contact).

<bag 
  name="_contact" 
  table="UserContacts" 
  lazy="true" 
  inverse="true" 
  cascade="all-delete-orphan" >

    <key column="UserId" />
    <one-to-many class="Contact" />
</bag>

In Contact mapping define one-to-one:

<one-to-one name="_user" class="User" constrained="true" />

In the database you need to have PK Users.Id and one (!) foreign key Contacts.UserID.

2) Another option is to simply have many-to-one in User mapping and one FK Users.ContactId

<many-to-one 
   name="_contact" 
   column="ContactId" 
   cascade="all-delete-orphan" 
   unique="true" 
   lazy="proxy"/>

Either way the maintenance that you asked about is not needed and anomalies are not possible.

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