Skip to content

Commit 4cf8cda

Browse files
Fix #1165 - Bidirectional, indexed collections documentation
1 parent fa81f63 commit 4cf8cda

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

doc/reference/modules/collection_mapping.xml

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,12 +852,6 @@ int accessLevel = permissions["accounts"]; // Error!]]></programlisting>
852852

853853
</para>
854854

855-
<para>
856-
Please note that NHibernate does not support bidirectional one-to-many associations
857-
with an indexed collection (list, map or array) as the "many" end, you have to
858-
use a set or bag mapping.
859-
</para>
860-
861855
<para>
862856
You may specify a bidirectional many-to-many association simply by mapping two
863857
many-to-many associations to the same database table and declaring one end as
@@ -937,6 +931,75 @@ session.Update(category); // The relationship will be saved]]></
937931

938932
</sect1>
939933

934+
<sect1 id="collections-indexedbidirectional">
935+
<title>Bidirectional associations with indexed collections</title>
936+
937+
<para>
938+
There are some additional considerations for bidirectional mappings with indexed collections
939+
(where one end is represented as a <literal>&lt;list&gt;</literal> or <literal>&lt;map&gt;</literal>)
940+
when using NHibernate mapping files. If there is a property of the child class that maps to the
941+
index column you can use <literal>inverse="true"</literal> on the collection mapping:
942+
</para>
943+
944+
<programlisting><![CDATA[<class name="Parent">
945+
<id name="Id" column="parent_id"/>
946+
....
947+
<map name="Children" inverse="true">
948+
<key column="parent_id"/>
949+
<map-key column="name"
950+
type="string"/>
951+
<one-to-many class="Child"/>
952+
</map>
953+
</class>
954+
955+
<class name="Child">
956+
<id name="Id" column="child_id"/>
957+
....
958+
<property name="Name" column="name"
959+
not-null="true"/>
960+
<many-to-one name="Parent"
961+
class="Parent"
962+
column="parent_id"
963+
not-null="true"/>
964+
</class>]]></programlisting>
965+
966+
<para>
967+
If there is no such property on the child class, the association cannot be considered truly
968+
bidirectional. That is, there is information available at one end of the association that is not
969+
available at the other end. In this case, you cannot map the collection
970+
<literal>inverse="true"</literal>. Instead, you could use the following mapping:
971+
</para>
972+
973+
<programlisting><![CDATA[<class name="Parent">
974+
<id name="Id" column="parent_id"/>
975+
....
976+
<map name="Children">
977+
<key column="parent_id"
978+
not-null="true"/>
979+
<map-key column="name"
980+
type="string"/>
981+
<one-to-many class="Child"/>
982+
</map>
983+
</class>
984+
985+
<class name="Child">
986+
<id name="Id" column="child_id"/>
987+
....
988+
<many-to-one name="Parent"
989+
class="Parent"
990+
column="parent_id"
991+
insert="false"
992+
update="false"
993+
not-null="true"/>
994+
</class>]]></programlisting>
995+
996+
<para>
997+
Note that in this mapping, the collection-valued end of the association is responsible for
998+
updates to the foreign key.
999+
</para>
1000+
1001+
</sect1>
1002+
9401003
<sect1 id="collections-ternary">
9411004
<title>Ternary Associations</title>
9421005

0 commit comments

Comments
 (0)