NHibernate:没有急切地获取相关属性

时间:2012-02-07 04:48:35

标签: c# nhibernate

问题

首先:NHibernate的新手,过去曾使用过Entity Framework。

我正在使用NHibernate 3.2,ASP.NET MVC3以及AutoMapper,并且发现了虽然

我在View中显示了正确的结果,但是对于返回的十行,我运行了42个SQL语句:   - 一个用于查询中的总行数   - 一排10排   - 两个用于返回的EACH行,即查询我的DmsUser表 - 我已禁用映射并发现它们对应于BookedOutDrawingControllerUser和BookedOutContractorUser一对多关系

我想知道为什么我在我的视图中显示的用户名没有在初始查询中被选中以获取行,以及我如何解决问题。

这是对现有应用程序的重写,现有应用程序中使用的解决方案是存储过程,但因为整个应用程序只有三个过程。由于其余的查询都是基于NHibernate标准的,我想摆脱现有的过程。

关闭实体:

public class Drawing
{
    public virtual int Id { get; set; }
    ...
    public virtual string BookedOutContractor { get; set; }
    public virtual DmsUser BookedOutDrawingControllerUser { get; set; }
    public virtual DmsUser BookedOutContractorUser { get; set; }
    public virtual string BookedOutDrawingControllerUsername { get; set; }
    public virtual string BookedOutContractorUserName { get; set; }
}

public partial class DmsUser
{
    public virtual string UserName { get; set; }
    public virtual string FirstName { get; set; }
    ...
}

编辑:添加完整查询 的查询

IList<Drawing> results = _session.CreateCriteria<Drawing>()
            .criteria.SetFirstResult(startIndex)
            .Add(Restrictions.InsensitiveLike("Vault", x))
            .Add(Restrictions.InsensitiveLike("Discipline", y))
            .SetFetchMode("BookedOutDrawingControllerUser", FetchMode.Eager)
            .SetFetchMode("BookedOutContractorUser", FetchMode.Eager)
            .SetMaxResults(pageLength).List<Drawing>();

生成SQL

查询数据页面:

SELECT TOP (10 /* @p0 */) this_.ID                                 as ID5_2_,
             this_.[relative path]                    as column2_5_2_,
             this_.dmsDocumentId                      as dmsDocum3_5_2_,
             this_.[Internal Name]                    as column4_5_2_,
             this_.title                              as title5_2_,
             this_.[Drawing Number]                   as column6_5_2_,
             this_.[Drawing Revision]                 as column7_5_2_,
             this_.[Drawing Discipline]               as column8_5_2_,
             this_.[Location Name]                    as column9_5_2_,
             this_.fileName                           as fileName5_2_,
             this_.[Booked Out]                       as column11_5_2_,
             this_.[Booked Out To]                    as column12_5_2_,
             this_.[Booked Out Date]                  as column13_5_2_,
             this_.url                                as url5_2_,
             this_.[Booked Out Contractor]            as column15_5_2_,
             this_.BookedOutDrawingControllerUsername as BookedO16_5_2_,
             this_.BookedOutContractorUsername        as BookedO17_5_2_,
             this_.[booked Out Due Date]              as column18_5_2_,
             this_.vault                              as vault5_2_,
             this_.BookedOutRequestID                 as BookedO20_5_2_,
             dmsuser2_.ID                             as ID0_0_,
             dmsuser2_.userName                       as userName0_0_,
             dmsuser2_.[first name]                   as column3_0_0_,
             dmsuser2_.[last name]                    as column4_0_0_,
             dmsuser2_.[phone number]                 as column5_0_0_,
             dmsuser2_.[mobile Number]                as column6_0_0_,
             dmsuser2_.[email address]                as column7_0_0_,
             dmsuser2_.Sponsored                      as Sponsored0_0_,
             dmsuser2_.Sponsoror                      as Sponsoror0_0_,
             dmsuser2_.[Access Type]                  as column10_0_0_,
             dmsuser2_.[Access Level]                 as column11_0_0_,
             dmsuser2_.LastLogin                      as LastLogin0_0_,
             dmsuser2_.[Date Registered]              as column13_0_0_,
             dmsuser2_.Project                        as Project0_0_,
             dmsuser2_.ActiveUser                     as ActiveUser0_0_,
             dmsuser2_.CompanyId                      as CompanyId0_0_,
             dmsuser3_.ID                             as ID0_1_,
             dmsuser3_.userName                       as userName0_1_,
             dmsuser3_.[first name]                   as column3_0_1_,
             dmsuser3_.[last name]                    as column4_0_1_,
             dmsuser3_.[phone number]                 as column5_0_1_,
             dmsuser3_.[mobile Number]                as column6_0_1_,
             dmsuser3_.[email address]                as column7_0_1_,
             dmsuser3_.Sponsored                      as Sponsored0_1_,
             dmsuser3_.Sponsoror                      as Sponsoror0_1_,
             dmsuser3_.[Access Type]                  as column10_0_1_,
             dmsuser3_.[Access Level]                 as column11_0_1_,
             dmsuser3_.LastLogin                      as LastLogin0_1_,
             dmsuser3_.[Date Registered]              as column13_0_1_,
             dmsuser3_.Project                        as Project0_1_,
             dmsuser3_.ActiveUser                     as ActiveUser0_1_,
             dmsuser3_.CompanyId                      as CompanyId0_1_
FROM   Drawings this_
   left outer join [User Details] dmsuser2_
     on this_.BookedOutDrawingControllerUsername = dmsuser2_.userName
   left outer join [User Details] dmsuser3_
     on this_.BookedOutContractorUsername = dmsuser3_.userName
WHERE  lower(this_.vault) like '%standard%' /* @p1 */
   and lower(this_.[Drawing Discipline]) like '%e%' /* @p2 */
ORDER  BY this_.title asc

重复查询20次:

SELECT dmsuser0_.ID                as ID0_0_,
   dmsuser0_.userName          as userName0_0_,
   dmsuser0_.[first name]      as column3_0_0_,
   dmsuser0_.[last name]       as column4_0_0_,
   dmsuser0_.[phone number]    as column5_0_0_,
   dmsuser0_.[mobile Number]   as column6_0_0_,
   dmsuser0_.[email address]   as column7_0_0_,
   dmsuser0_.Sponsored         as Sponsored0_0_,
   dmsuser0_.Sponsoror         as Sponsoror0_0_,
   dmsuser0_.[Access Type]     as column10_0_0_,
   dmsuser0_.[Access Level]    as column11_0_0_,
   dmsuser0_.LastLogin         as LastLogin0_0_,
   dmsuser0_.[Date Registered] as column13_0_0_,
   dmsuser0_.Project           as Project0_0_,
   dmsuser0_.ActiveUser        as ActiveUser0_0_,
   dmsuser0_.CompanyId         as CompanyId0_0_
FROM   [User Details] dmsuser0_
WHERE  dmsuser0_.userName = '' /* @p0 */

0 个答案:

没有答案