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>