Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This certainly is possible. The Hibernate (not NHibernate) documentation does a good job of explaining this. <a href="http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html#collections-indexedbidirectional" rel="nofollow">7.3.3. Bidirectional associations with indexed collections</a>. There is an issue in the NHibernate bug tracker to have <a href="http://nhforge.org/doc/nh/en/index.html#collections-bidirectional" rel="nofollow">this error</a> in the documentation corrected: <a href="https://nhibernate.jira.com/browse/NH-3554" rel="nofollow">NH-3554</a></p> <p>Below is a complete example of how to do this...</p> <h1>Example</h1> <h2>Entity Classes</h2> <pre><code>public class Parent { public virtual int Id { get; private set; } private IDictionary&lt;string, Child&gt; _children = new Dictionary&lt;string, Child&gt;(); public virtual IDictionary&lt;string, Child&gt; Children { get { return _children; } private set { _children = value; } } public virtual void AddChild(Child child) { child.Parent = this; _children[child.Name] = child; } } public class Child { public virtual int Id { get; private set; } public virtual Parent Parent { get; protected internal set; } public virtual string Name { get; set; } } </code></pre> <h2>NHibernate Mappings</h2> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="so" namespace="so.Q3398624"&gt; &lt;class name="Parent"&gt; &lt;id name="Id"&gt; &lt;column name="Id" /&gt; &lt;generator class="hilo" /&gt; &lt;/id&gt; &lt;map name="Children" inverse="true" cascade="all-delete-orphan"&gt; &lt;key column="ParentId" /&gt; &lt;index column="Name" type="string" /&gt; &lt;one-to-many class="Child" /&gt; &lt;/map&gt; &lt;/class&gt; &lt;class name="Child"&gt; &lt;id name="Id"&gt; &lt;column name="Id" /&gt; &lt;generator class="hilo" /&gt; &lt;/id&gt; &lt;many-to-one name="Parent" column="ParentId" cascade="save-update" not-null="true" unique-key="U1" /&gt; &lt;property name="Name" not-null="true" unique-key="U1" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <h2>Tables</h2> <pre><code>CREATE TABLE dbo.Parent ( Id int NOT NULL PRIMARY KEY ); CREATE TABLE dbo.Child ( Id int NOT NULL PRIMARY KEY, ParentId int NOT NULL, Name nvarchar(255) NOT NULL, FOREIGN KEY (ParentId) REFERENCES dbo.Parent (Id), UNIQUE (ParentId, Name) ); </code></pre> <h2>Insert some records...</h2> <pre><code>var parent = new Parent(); parent.AddChild(new Child { Name = "abc" }); parent.AddChild(new Child { Name = "123" }); session.Save(parent); </code></pre> <h1>Final Notes</h1> <p>Be careful when changing a Child's Name. I think the best approach would be to remove the Child from the Parent, change its name, and finally re-add it to the parent. Otherwise <code>Child.Name</code> and <code>Parent.Children.Keys</code> would be out of sync.</p>
 

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