I am afraid, that his won't work. By design and the nature of the <join>
. As stated in the documentation:
Using the
<join>
element, it is possible to map properties of one class to several tables, when there's a 1-to-1 relationship between the tables.
The way how to (I do) solve this issue is a bit different. I have an object Language
, the Option has colleciton of languages
public virtual IList<Language> Languages {get; set;}
The mapping is for example <bag>
<bag name="Languages" batch-size="25"
cascade="all-delete-orphan" inverse="true" >
<key column="option_id" />
<one-to-many class="Language" />
<filter name="LanguagFilter" condition=":languageId = LanguageId" />
</bag>
The trick is in the filter. This is a dynamic version of the where mapping-attribute (18.1. NHibernate filters)
<filter-def name="LanguageFilter" >
<filter-param name="languageId" type="Int32" />
</filter-def>
Then, we can turn the filter on, for all operations of the current session. Only once per request (if web app), inside some AOP, where we know the language id:
var filter = session.EnableFilter("LanguageFilter");
filter.SetParameter("languageId", theCurrentLanguageIdFromUser);
And finally, we know, that the collection Languages contains only one record and we can always access .First()
. No more multiple results with more languages
Also see: https://stackoverflow.com/a/16625867/1679310, https://stackoverflow.com/a/18479266/1679310