У меня есть механизм перевода, отображенный следующим образом:
<class name="Core.Model.Entities.Translation, Core.Model" table="translation" lazy="false">
<id name="Id" column="id" type="Int64">
<generator class="native" />
</id>
<map name="Translations" table="translation_value" inverse="true" fetch="join" cascade="all-delete-orphan" lazy="false">
<key column="translation_id" />
<index-many-to-many column="language_id" class="Core.Model.Entities.Language, Core.Model"/>
<one-to-many class="Core.Model.Entities.TranslationValue, Core.Model"/>
</map>
</class>
<class name="Core.Model.Entities.TranslationValue, Core.Model" table="translation_value" lazy="false">
<id name="Id" column="id" type="Int64">
<generator class="native" />
</id>
<property name="Value" column="value" type="String"/>
<many-to-one name="Translation" column="translation_id" class="Core.Model.Entities.Translation, Core" not-null="true"/>
<many-to-one name="Language" column="language_id" class="Core.Model.Entities.Language, Core" not-null="true" />
В примере класса "item" используются такие переводы:
<class name="Core.Model.Entities.Item, Core.Model" table="item" lazy="true">
<id name="Id" column="id" type="long">
<generator class="native" />
</id>
<property name="Symbol" column="symbol"/>
<many-to-one name="Name" class="Core.Model.Entities.Translation, Core.Model" fetch="join" column="name" cascade="all" />
<many-to-one name="Description" class="Core.Model.Entities.Translation, Core.Model" fetch="join" column="description" cascade="all" />
</class>
Отлично работает,НО, когда один элемент имеет несколько переводов, метод Item.Dao.GetAll () возвращает столько же результатов, сколько существует переводов, поэтому, если я добавлю 3 перевода в Item.Name, метод Item.Dao.GetAll вернет мне 3 идентичных объекта элемента.Я проверил базу данных - все в порядке - одна строка в таблице "item", одна строка в таблице "translation" и три строки в таблице "translation_value".Почему Nhibernate возвращает мне 3 результата, когда в базе данных есть только 1 запись!?
РЕДАКТИРОВАТЬ: Я посмотрел на запрос, который генерирует NHibernate, и я похож на это:
SELECT
this_.id as id23_3_,
this_.symbol as symbol23_3_,
this_.name as name23_3_,
this_.description as descript4_23_3_,
this_.sort as sort23_3_,
this_.published as published23_3_,
this_.created_at as created7_23_3_,
this_.updated_at as updated8_23_3_,
translatio2_.id as id26_0_,
translatio3_.translation_id as translat3_5_,
translatio3_.id as id5_,
translatio3_.language_id as language4_5_,
translatio3_.id as id9_1_,
translatio3_.value as value9_1_,
translatio3_.translation_id as translat3_9_1_,
translatio3_.language_id as language4_9_1_,
translatio4_.id as id26_2_ FROM item this_
left outer join translation translatio2_ on this_.name=translatio2_.id
left outer join translation_value translatio3_ on translatio2_.id=translatio3_.translation_id
left outer join translation translatio4_ on this_.description=translatio4_.id
Этот запрос ISвозвращает 3 результата, по одному на каждый перевод.Но почему nhibernate не заполняет много-к-одному объектам переводов, а не возвращает три строки?