Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I think this should be Many-to-Many relationship</p> </blockquote> <p>You are correct. In your scenario you will need to use a self-referential many-to-many mapping. But using a single <em>Users</em> table you cannot represent the relation between <em>users</em> and <em>friends</em> (using a single table you could represent a self-referential <a href="http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/#example-parentchild" rel="noreferrer">parent-child</a> relationship). You will need an intermediary table to achieve this. Here's an example using <a href="http://www.sqlite.org/" rel="noreferrer">SQLite</a> ADO.NET <a href="http://sqlite.phxsoftware.com/" rel="noreferrer">provider</a> to show one possible way of modeling your scenario:</p> <p>User.hbm.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test" assembly="test"&gt; &lt;class name="User" table="Users"&gt; &lt;id name="Id" column="id"&gt; &lt;generator class="native"/&gt; &lt;/id&gt; &lt;property name="Name" column="name"/&gt; &lt;set name="Friends" table="Friends"&gt; &lt;key column="user_id"/&gt; &lt;many-to-many class="User" column="friend_id"/&gt; &lt;/set&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>From the above mapping you will notice the use of the following tables: Users and Friends</p> <p>And here's the code:</p> <pre><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&lt;User&gt;(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&lt;User&gt; Friends { get; set; } } } </code></pre> <p>And finally for the sake of completeness here's my config file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /&gt; &lt;/configSections&gt; &lt;hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"&gt; &lt;session-factory&gt; &lt;property name="connection.provider"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/property&gt; &lt;property name="connection.driver_class"&gt;NHibernate.Driver.SQLite20Driver&lt;/property&gt; &lt;property name="dialect"&gt;NHibernate.Dialect.SQLiteDialect&lt;/property&gt; &lt;property name="connection.connection_string"&gt;Data Source=nhibernate.db;Version=3&lt;/property&gt; &lt;mapping assembly="test" /&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; &lt;/configuration&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload