为什么NHibernate从一个数据库行返回多个结果?

时间:2010-11-15 11:27:05

标签: nhibernate internationalization nhibernate-mapping

我的翻译引擎映射如下:

<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”表中的三行。当数据库中只有1个条目时,为什么Nhibernate会返回3个结果!

编辑: 我查看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

此查询返回3个结果,每个结果一个。但是为什么nhibernate不会填充多个到一个翻译对象而不是返回三行?

2 个答案:

答案 0 :(得分:2)

这是因为您在Translations集合的映射中指定了fetch="join"。如果您不想对集合使用延迟加载,那么您应该更改Dao.GetAll()方法以使用HQL或Criteria API来急切地获取集合并将结果限制为不同的父对象。

答案 1 :(得分:1)

这取决于GetAll()方法的实现方式。如果您使用的是标准API,请使用

 criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());

HQL有一个明确的条款。