我正在尝试使用 Nhibernate 作为我的 ORM 的社交网络类型场景。
我有一个用户表和一个朋友表。
User Table
----------
UserID
FirstName
LastName
Email
Friend Table
-------------
UserID
FriendUserID
AddedDateTime
这意味着在朋友表中它可能有很多记录,比如
A - B (A initiated a friend request to B, accepted)
A - C (A initiated a friend request to C, accepted)
B - C
D - A (D initiated a friend request to A, accepted)
B - E
我怎样才能拥有一个 Nhibernate 属性,这样如果我为 A 调用 userObject.Friends,我将 B、C 和 D 作为用户对象返回。
为用户对象映射 xml...
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Namespace.Data.Entities.User,Namespace.Data.Entities" table="`User`" lazy="true">
<id name="UserId" column="`UserId`" type="Guid">
<generator class="assigned" />
</id>
<property type="string" length="150" name="FirstName" column="`FirstName`" />
<property type="string" length="150" name="LastName" column="`LastName`" />
<property type="string" not-null="true" length="256" name="Email" column="`Email`" />
<bag name="UsersFriends" inverse="true" lazy="true" cascade="all">
<key column="`UserID`" />
<one-to-many class="Namespace.Data.Entities.Friend,Namespace.Data.Entities" />
</bag>
<bag name="FriendsofUser" inverse="true" lazy="true" cascade="all">
<key column="`FriendUserID`" />
<one-to-many class="Namespace.Data.Entities.Friend,Namespace.Data.Entities" />
</bag>
</class>
</hibernate-mapping>
和朋友对象...
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Namespace.Data.Entities.Friend,Namespace.Data.Entities" table="`Friend`" lazy="true">
<id name="Id" column="`ID`" type="int">
<generator class="native" />
</id>
<property type="DateTime" not-null="true" name="AddedDateTime" column="`AddedDateTime`" />
<many-to-one name="User" cascade="none" column="`UserID`" not-null="true" />
<many-to-one name="FriendUser" cascade="none" column="`FriendUserID`" not-null="true" />
</class>
</hibernate-mapping>