Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nhibernate hql查询带来太多数据_Nhibernate_Hql - Fatal编程技术网

Nhibernate hql查询带来太多数据

Nhibernate hql查询带来太多数据,nhibernate,hql,Nhibernate,Hql,我希望下面的查询返回一个Employee对象,该对象的分配在请求的日期范围内 但查询返回所有分配。它还对数据库进行了三次查询,我不确定我是否理解(第三次可能是因为我对活动主题的批处理大小为100) 如何更改此查询以有效地仅返回请求期间的分配 干杯, 贝里尔 数据库SQL: NHibernate: select resource0_.ResourceId as ResourceId0_, resource0_.BusinessId as BusinessId0_, resource0_.Resou

我希望下面的查询返回一个Employee对象,该对象的分配在请求的日期范围内

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

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

干杯,
贝里尔

数据库SQL:

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查询

    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>();

    }
公共资源getResourceForDaterRange(字符串businessId,日期范围周期),其中T:Resource
{
勾选.RequireStringValue(businessId,“businessId”);
支票。要求全部(期间);
常量字符串hql=
@"
选择r
来自资源r
内部连接r.分配作为
其中r.BusinessId=:BusinessId
和a.TimeRange.StartTime介于:periodStart和:periodEnd之间”;
return\u session.CreateQuery(hql)
.SetString(“businessId”,businessId)
.SetDateTime(“periodStart”,period.Start)
.SetDateTime(“periodEnd”,period.End)
.UniqueResult();
}
映射

<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>


...

它执行3个查询的原因可能是由于延迟加载被关闭。因此,它正在加载与实体资源关联的所有对象。这就是它获得所有分配的原因

试着做一个获取连接

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

类的延迟加载没有关闭,但是fetch将它与预期结果一起返回到一个查询。谢谢