同一实体中的 2 个外键映射到同一主键问题

发布于 2024-09-12 22:01:27 字数 1023 浏览 0 评论 0 原文

您好,希望有人可以帮助我。

我有这样的情况: 用户实体存储我的所有用户(包括管理员)。 评论实体,存储针对用户的所有评论。

我的问题是我还在评论实体中存储发表评论的管理员的用户 ID。所以现在我的 Comment 实体有 2 个 userId 字段,并且在检索 User 实体时,它不会获取任何关联的评论。

我的映射代码如下:

在 User 实体内..

<set name="MemberComments" cascade="all-delete-orphan" inverse="true">
  <key column="UserCommentId"/>
  <one-to-many class="IlluminatiCoreUserComment"/>
</set>

在 UserComment 实体内

<id name="UserCommentId">
  <generator class="identity"/>
</id>
<many-to-one name="User" class="IlluminatiCoreMember" column="UserId" not-null="true"/>
<property name="Comment" not-null="true"/>
<property name="CreatedTimeStamp" not-null="true"/>
<property name="ModifiedTimeStamp" not-null="true"/>
<many-to-one name="CommentedByUser" class="IlluminatiCoreMember" column="CommentedByUserId" not-null="true"  inverse="false"/>

如何使 User 实体内的 MemberComments 仅指向 User 多对一字段?

感谢您提前的帮助

Hello hope someone can help me on this.

I have a situation where I have:
User entity which stores all my users including admins.
Comments entity which stores all comment against users.

my problem is I'm also storing within the comment entity the user id of the admin who made the comment. So now I have the Comment entity having 2 userId fields and when retrieving a User entity it wouldn't fetch any associating comments.

My mapping code is as below:

Within the User Entity..

<set name="MemberComments" cascade="all-delete-orphan" inverse="true">
  <key column="UserCommentId"/>
  <one-to-many class="IlluminatiCoreUserComment"/>
</set>

Within the UserComment Entity

<id name="UserCommentId">
  <generator class="identity"/>
</id>
<many-to-one name="User" class="IlluminatiCoreMember" column="UserId" not-null="true"/>
<property name="Comment" not-null="true"/>
<property name="CreatedTimeStamp" not-null="true"/>
<property name="ModifiedTimeStamp" not-null="true"/>
<many-to-one name="CommentedByUser" class="IlluminatiCoreMember" column="CommentedByUserId" not-null="true"  inverse="false"/>

How can I make MemberComments within the User entity point to just the User many-to-one field?

Thanks for your help in advance

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

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

发布评论

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

评论(1

终陌 2024-09-19 22:01:27

User 中的 元素应该具有 User 的 FK,而不是 UserComment 的 id。

换句话说,与多对一、UserId 相同的列。

The <key> element in User should have the FK to User, not the id of UserComment.

In other words, the same column as the many-to-one, UserId.

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