开发者

2 foreign keys in same entity mapping to same primary key problem

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. 开发者_JAVA百科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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜