我正在尝试使用Nhibernate作为我的ORM的社交网络类型场景。
我有一个User表和一个Friend表。
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>
答案 0 :(得分:2)
我建议
public class User
{
public virtual long Id { get; set; }
public virtual ICollection<FriendshipRequest> InitiatedRequests { get; private set; }
public virtual ICollection<FriendshipRequest> ReceivedRequests { get; private set; }
public virtual IEnumerable<User> Friends
{
get {
return (from fr in InitiatedRequests
where fr.Accepted
select fr.Friend)
.Concat(
from fr in ReceivedRequests
where fr.Accepted
select fr.Initiator);
}
}
public User()
{
InitiatedRequests = new HashSet<FriendshipRequest>();
ReceivedRequests = new HashSet<FriendshipRequest>();
}
}
public class FriendshipRequest
{
public virtual User Initiator { get; set; }
public virtual User Friend { get; set; }
public virtual bool Accepted { get { return Added != default(DateTime); } }
public virtual DateTime Added { get; set; }
// override Equals
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Namespace.Data.Entities.User,Namespace.Data.Entities" table="`User`">
...
<set name="InitiatedRequests" cascade="all" table="FriendshipRequests">
<key column="`UserID`" />
<composite-element>
<property type="DateTime" name="Added" column="`AddedDateTime`" />
<parent name="Initiator" />
<many-to-one name="Friend" column="`FriendUserID`" not-null="true" />
</composite-element>
</bag>
<set name="ReceivedRequests" cascade="all" table="FriendshipRequests" inverse="true">
<key column="`FriendUserID`" />
<composite-element>
<property type="DateTime" name="Added" column="`AddedDateTime`" />
<parent name="Friend" />
<many-to-one name="Initiator" column="`UserID`" not-null="true" />
</composite-element>
</bag>
</class>
</hibernate-mapping>