Nhibernate mapping - User and Friends

不羁岁月 提交于 2019-12-25 05:13:08

问题


I am trying a social network type scenario using Nhibernate as my ORM.

I have a User table and a Friend table.

User Table
----------
UserID
FirstName
LastName
Email

Friend Table
-------------
UserID
FriendUserID
AddedDateTime

This means the in the Friend Table it could have many to many records like

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

How can I have a Nhibernate property such that if I call userObject.Friends for A, I get back B,C and D as user objects.

Mapping xml for User Object...

<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>

and Friend object...

<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>

回答1:


i would suggest

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>
  • for the collection ReceivedRequests the inverse must be set to true so that this collection is only handled from one side
  • i would take set so each Friendship can only be added once (which properties are unique depends on equals), it makes sense and NHibernate can optimise some things
  • to avoid Select n+1, if you plan to use the friends collection make sure you eager load the other collections too


来源:https://stackoverflow.com/questions/10807776/nhibernate-mapping-user-and-friends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!