Linq到NHibernate生成不必要的JOIN

时间:2011-09-03 06:45:31

标签: nhibernate linq-to-nhibernate

我正在使用NHibernate 3.2.1。以下查询

return session.Query<TmTranslation>()
              .Where(x => x.TranslationUnit.Document.Job == job)
              .OrderBy(x => x.Id)
              .ToList();

产生这个SQL:

select   tmtranslation.id,
         tmtranslation.text,
         tmtranslation.fk_id_translation_unit

from     "TRANSLATION" tmtranslation
         inner join "TRANSLATION_UNIT" tmunit
           on tmtranslation.fk_id_translation_unit = tmunit.id
         inner join "TRANSLATION_UNIT" tmunit2
           on tmtranslation.fk_id_translation_unit = tmunit2.id
         inner join "DOCUMENT" tmdocument
           on tmunit2.fk_id_document = tmdocument.id
where    tmdocument.fk_id_job = 174
order by tmtranslation.id asc

我的映射:

public class TmTranslationMap : ClassMap<TmTranslation>
{
    public TmTranslationMap()
    {
        Table("\"TRANSLATION\"");
        LazyLoad();            
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translation", "200");            
        Map(x => x.Text).Column("text");
        References<TmTranslationUnit>(x => x.TranslationUnit, "fk_id_translation_unit").Cascade.None();
        DynamicUpdate();
    }
}

public class TmTranslationUnitMap: ClassMap<TmTranslationUnit>
{
    public TmTranslationUnitMap()
    {
        Table("\"TRANSLATION_UNIT\"");
        LazyLoad();    
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translation_unit", "200");
        HasMany(x => x.Translations).Inverse().KeyColumn("fk_id_translation_unit").Cascade.None();
        References<TmDocument>(x => x.Document, "fk_id_document").Not.Nullable().Cascade.None();
    }
}

public class TmDocumentMap : ClassMap<TmDocument>
{
    public TmDocumentMap()
    {
        Table("\"DOCUMENT\"");
        LazyLoad();          
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_document", "50");
        References<TmJob>(x => x.Job, "fk_id_job").Not.Nullable();
        HasMany(x => x.TranslationUnits).Inverse().KeyColumn("fk_id_document").Cascade.SaveUpdate();
    }
}

正如您所看到的,其中一个JOIN是无用的,只会使查询运行得更慢。有什么办法可以让查询不使用Linq生成不必要的JOIN吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

似乎LINQ to NHibernate提供程序无法导航从目标到源表的关联,并且每次遇到这样的关联时都会生成一个单独的连接:一个用于翻译 - &gt; TranslationUnit和TranslationUnit to Document。

技巧是帮助提供者向另一个方向导航:文档 - &gt; TranlationUnit - &gt;像这样的翻译:

var items=(from doc in session.Query<Document>()
            from tu in doc.TranslationUnits
                from translation in tu.Translations
           where doc.Job ==job                        
           orderby translation.Id
           select translation).ToList();

生成的SQL查询是:

SELECT     translatio2_.Id as Id1_, translatio2_.Text as Text1_, translatio2_.TmTranslationUnitId as TmTransl3_1_
FROM         [Document] AS document0_ INNER JOIN
                  TmTranslationUnit AS translatio1_ ON document0_.Id = translatio1_.DocumentId INNER JOIN
                  TmTranslation AS translatio2_ ON translatio1_.Id = translatio2_.TmTranslationUnitId
WHERE     (document0_.JobId = @p0)
ORDER BY translatio2_.Id