internationalization - Why NHibernate returns multiple results from one database row? -
i have translation engine mapped 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 translations 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, when single item has multiple translations, item.dao.getall() method, returns many results there translations, if add 3 translations item.name, item.dao.getall method returns me 3 identical item objects. have checked database - fine - 1 row in "item" table, 1 row in "translation" table , 3 rows in "translation_value" table. why nhibernate returns me 3 results when there 1 entry in database!?
edit: looked on query wich nhibernate generates , looks this:
select this_.id id23_3_, this_.symbol symbol23_3_, this_.name name23_3_, this_.description descript4_23_3_, this_.sort sort23_3_, this_.published published23_3_, this_.created_at created7_23_3_, this_.updated_at updated8_23_3_, translatio2_.id id26_0_, translatio3_.translation_id translat3_5_, translatio3_.id id5_, translatio3_.language_id language4_5_, translatio3_.id id9_1_, translatio3_.value value9_1_, translatio3_.translation_id translat3_9_1_, translatio3_.language_id language4_9_1_, translatio4_.id id26_2_ 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 returning 3 results, 1 each translation. why nhibernate not fills many 1 translations objects instead of returning 3 rows?
it's happening because have specified fetch="join"
in mapping translations collection. if don't want use lazy loading collection, should change dao.getall() method use hql or criteria api eagerly fetch collection , limit result distinct parent objects.
Comments
Post a Comment