开发者

Why NHibernate returns multiple results from one database row?

I have translation engine mapped as follows:

<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" />

Example "item" class uses that translations like that:

<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>

It works great, BUT when single item has multiple translations, Item.Dao.GetAll() method, returns as many results as there are translations, so if I add 3 translations to Item.Name, Item.Dao.GetAll method returns me 3 identical item objects. I have checked the database - everything is fine - one row in "item" table, one row in "translation" table and three rows in "translation_value" table. Why Nhibernate returns me 3 results when there is only 1 entry in database!?

EDIT: I Looked up on query wich NHibernate generates and i looks like this:

SELECT 
this_.id as id23_3_, 
t开发者_JAVA百科his_.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

This query IS returning 3 results, one for each translation. But why nhibernate does not fills many to one translations objects instead of returning three rows?


It's happening because you have specified fetch="join" in the mapping for the Translations collection. If you don't want to use lazy loading for the collection, then you should change your Dao.GetAll() method to use HQL or the Criteria API to eagerly fetch the collection and limit the result to distinct parent objects.


It depends how the GetAll() method is implemented. If you are using criteria API, use

 criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());

HQL has a distinct clause.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜