hql查询引入了太多数据

时间:2011-01-01 18:12:11

标签: nhibernate hql

我希望下面的查询返回一个具有Allocations的Employee对象,该对象属于请求的日期范围。

查询返回所有分配。它还对数据库进行了三次查询,我不确定是否理解(第三种可能是因为我在活动主题上的批量大小为100)。

如何更改此查询以有效地仅返回请求期间的分配?

干杯,
Berryl

数据库SQLS:

NHibernate: select resource0_.ResourceId as ResourceId0_, resource0_.BusinessId as BusinessId0_, resource0_.ResourceName as Resource4_0_, resource0_.OwnerName as OwnerName0_, resource0_.EmployeeNumber as Employee6_0_, resource0_.FirstName as FirstName0_, resource0_.LastName as LastName0_, resource0_.DepartmentId as Departme9_0_, resource0_.ResourceType as Resource2_0_ from Resources resource0_ 
inner join Allocations allocation1_ on resource0_.ResourceId=allocation1_.ResourceId 
where resource0_.BusinessId=@p0 
and (allocation1_.StartTime between @p1 and @p2);@p0 = '000001' [Type: String (0)], @p1 = 12/27/2010 12:00:00 AM [Type: DateTime (0)], @p2 = 1/2/2011 11:59:59 PM [Type: DateTime (0)]

NHibernate: SELECT allocation0_.ResourceId as ResourceId1_, allocation0_.AllocationId as Allocati1_1_, allocation0_.AllocationId as Allocati1_2_0_, allocation0_.ActivitySubjectId as Activity2_2_0_, allocation0_.ResourceId as ResourceId2_0_, allocation0_.StartTime as StartTime2_0_, allocation0_.EndTime as EndTime2_0_, allocation0_.PostingTime as PostingT6_2_0_ 
FROM Allocations allocation0_ WHERE allocation0_.ResourceId=@p0;@p0 = 98304 [Type: Int32 (0)]

NHibernate: SELECT activitysu0_.ActivitySubjectId as Activity1_3_0_, activitysu0_.BusinessId as BusinessId3_0_, activitysu0_.Description as Descript4_3_0_, activitysu0_.ActivitySubjectType as Activity2_3_0_ 
FROM ActivitySubjects activitysu0_ WHERE activitysu0_.ActivitySubjectId in (@p0, @p1, @p2, @p3);@p0 = 32784 [Type: Int32 (0)], @p1 = 32854 [Type: Int32 (0)], @p2 = 32860 [Type: Int32 (0)], @p3 = 32861 [Type: Int32 (0)]

HQL QUERY

    public Resource GetResourceForDateRange<T>(string businessId, DateRange period) where T : Resource
    {
        Check.RequireStringValue(businessId, "businessId");
        Check.RequireNotNull(period);

        const string hql =
            @"
                                select r 
                                from Resource r 
                                inner join r.Allocations as a
                                where r.BusinessId = :businessId 
                                and a.TimeRange.StartTime between :periodStart and :periodEnd";

        return _session.CreateQuery(hql)
            .SetString("businessId", businessId)
            .SetDateTime("periodStart", period.Start)
            .SetDateTime("periodEnd", period.End)
            .UniqueResult<Resource>();

    }

MAPPING

<id name="Id" type="System.Int32" unsaved-value="0">
  <column name="ResourceId" />
  <generator class="hilo" />
</id>

<discriminator column="ResourceType" type="System.String" />

<property name="BusinessId" length="50" not-null="true" unique="true" unique-key="DomainSignature" index="ResourceDomainSignature" />
<property name="ResourceName" length="75" not-null="true" />
<property name="OwnerName" length="75" not-null="true" />

<set access="field.camelcase-underscore" cascade="all-delete-orphan" inverse="true" name="Allocations">
  <key foreign-key="Allocations_Resource_FK">
    <column name="ResourceId" />
  </key>
  <one-to-many class="Allocations.Allocation" />
</set>

<subclass name="Resources.HumanResources.Employee" discriminator-value="EMPLOYEE">
    ...
</subclass>

1 个答案:

答案 0 :(得分:1)

它进行3次查询的原因可能是由于延迟加载关闭。所以它正在加载与实体资源相关的所有对象。这就是它获得所有分配的原因。

尝试进行获取加入

from Resource r 
left join fetch r.Allocations as a
where r.BusinessId = :businessId 
and a.TimeRange.StartTime between :periodStart and :periodEnd