Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8009669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:30:49+00:00 2026-06-04T18:30:49+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:30:50+00:00Added an answer on June 4, 2026 at 6:30 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to solve a bug! I have an ajax powered social network (with like
I am trying to get a notification when an user comments using the social
I am trying to create a social network application using elgg.Since i am pretty
I'm trying to construct a social network graph of twitter users who have mentioned
I have a social network The users table is around 60,000 rows The friends
I have a social network that I am making using php and mysql. I
I'm trying to create a social networking type of website where a user can
I'm trying to create a social-network like feature in an app I'm building, and
I am trying to integrate facebook connect with nmy social network site, currently users
I have a social network for the cystic fibrosis community. There is a section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.