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 101579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:47:54+00:00 2026-05-11T00:47:54+00:00

Please help! I couldn’t figure it out how to map the following situation: I

  • 0

Please help! I couldn’t figure it out how to map the following situation:

I have only 1 table.

[Table] User { id, name }

My class look like this

public class User {   public int Id { get; set; }   public string Name { get; set; }    public ISet<User> Friends { get; set; } } 

Each user has relationship with other users. e.g.’User A’ can has many friends which is other User.

What should be the mapping for this? I think this should be Many-to-Many relationship but I don’t really know how the HBM will look like?

Thanks,

  • 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. 2026-05-11T00:47:54+00:00Added an answer on May 11, 2026 at 12:47 am

    I think this should be Many-to-Many relationship

    You are correct. In your scenario you will need to use a self-referential many-to-many mapping. But using a single Users table you cannot represent the relation between users and friends (using a single table you could represent a self-referential parent-child relationship). You will need an intermediary table to achieve this. Here’s an example using SQLite ADO.NET provider to show one possible way of modeling your scenario:

    User.hbm.xml:

    <?xml version='1.0' encoding='utf-8' ?> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'     namespace='Test' assembly='test'>      <class name='User' table='Users'>         <id name='Id' column='id'>             <generator class='native'/>         </id>         <property name='Name' column='name'/>         <set name='Friends' table='Friends'>             <key column='user_id'/>             <many-to-many class='User' column='friend_id'/>         </set>     </class>  </hibernate-mapping> 

    From the above mapping you will notice the use of the following tables: Users and Friends

    And here’s the code:

    using System; using System.IO; using System.Collections.Generic; using System.Data.SQLite; using NHibernate; using NHibernate.Cfg; using Iesi.Collections.Generic;  namespace Test {     class Program     {         public static void Main()          {             if (File.Exists('nhibernate.db'))             {                 File.Delete('nhibernate.db');             }             ExecuteCommand('create table Users (id integer, name string)');             ExecuteCommand('create table Friends (user_id integer, friend_id string)');              ExecuteCommand('insert into Users (id, name) values (1, 'user1')');             ExecuteCommand('insert into Users (id, name) values (2, 'user2')');             ExecuteCommand('insert into Users (id, name) values (3, 'user3')');              // User1 is friend with User2                 ExecuteCommand('insert into Friends (user_id, friend_id) values (1, 2)');             // User1 is friend with User3                 ExecuteCommand('insert into Friends (user_id, friend_id) values (1, 3)');             // User2 is friend with User1                 ExecuteCommand('insert into Friends (user_id, friend_id) values (2, 1)');             // User3 is friend with User1                 ExecuteCommand('insert into Friends (user_id, friend_id) values (3, 1)');              ISessionFactory sessionFactory =                 new Configuration().Configure().BuildSessionFactory();             ISession session = sessionFactory.OpenSession();             User user = session.Get<User>(1);             Console.WriteLine(user.Friends.Count);              session.Close();             sessionFactory.Close();         }          private static void ExecuteCommand(string sql)         {             using (SQLiteConnection connection = new SQLiteConnection('Data Source=nhibernate.db;Version=3'))             using (SQLiteCommand command = new SQLiteCommand(sql, connection))             {                 connection.Open();                 command.ExecuteNonQuery();             }         }     }      class User      {         public virtual int Id { get; set; }         public virtual string Name { get; set; }         public virtual ISet<User> Friends { get; set; }     } } 

    And finally for the sake of completeness here’s my config file:

    <?xml version='1.0' encoding='utf-8' ?> <configuration>     <configSections>         <section             name='hibernate-configuration'             type='NHibernate.Cfg.ConfigurationSectionHandler, NHibernate'         />     </configSections>      <hibernate-configuration xmlns='urn:nhibernate-configuration-2.2'>         <session-factory>             <property name='connection.provider'>NHibernate.Connection.DriverConnectionProvider</property>             <property name='connection.driver_class'>NHibernate.Driver.SQLite20Driver</property>             <property name='dialect'>NHibernate.Dialect.SQLiteDialect</property>             <property name='connection.connection_string'>Data Source=nhibernate.db;Version=3</property>              <mapping assembly='test' />         </session-factory>     </hibernate-configuration> </configuration> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If you attempt to deterministically clear the contents of a… May 11, 2026 at 10:01 am
  • added an answer I like to think of the Single Responsibility Principle as… May 11, 2026 at 10:01 am
  • added an answer What you are describing is property sheets and property pages… May 11, 2026 at 10:01 am

Related Questions

Please help! I couldn't figure it out how to map the following situation: I
I've recently written this with help from SO. Now could someone please tell me
Please help! I'm really at my wits' end. My program is a little personal
Please help, I am stuck here --- irb> a = line of text\n line
Please help! Background info I have a WPF application which accesses a SQL Server
I might have wandered into wrong direction so please help. I have a page
I cannot understand how this is possible. Please help!! I have an app with
I know I'm not asking this quite right, either. Please help me better form
I am getting a little confused and need some help please. Take these two
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.