NHibernate - 具有复合id的“In”查询

时间:2010-07-15 11:41:29

标签: nhibernate linq-to-nhibernate

我正在尝试对id对象的集合执行“in”查询(实现为具有两个整数id成员的简单类),这些对象被映射为复合键,当我查询时,我看到一些奇怪的结果使用条件api使用Restrictions.In并使用NHibernate.Linq使用idList.Contains

以下是一个示例用法:

    Public Function GetByMultipleIds(ByVal ids As ICollection(Of QualificationKey)) As IList(Of Qualification) Implements IQualificationRepository.GetByMultipleIds
        Dim query = Session.CreateCriteria(Of Qualification)()
        query.Add(Restrictions.In("Id", ids.ToArray()))
        Return query.List(Of Qualification)()
    End Function

以下是我的密钥的映射:

    <composite-id name="Id" class="QualificationKey">
        <key-property name="QualificationAreaId" column="QualificationAreaId"/>
        <key-property name="QualificationLevelId" column="QualificationLevelId"/>
    </composite-id>

以下是生成的结果SQL:

SELECT this_.QualificationAreaId                                                                  as Qualific1_6_2_,
       this_.QualificationLevelId                                                                 as Qualific2_6_2_,
       this_.Version                                                                              as Version6_2_,
       this_.Rank                                                                                 as Rank6_2_,
       (SELECT QualificationArea.QualificationAreaTypeId
        FROM   QualificationArea
        WHERE  QualificationArea.QualificationAreaId = this_.QualificationAreaId) as clazz_2_,
       qualificat2_.QualificationAreaId                                                           as Qualific1_7_0_,
       qualificat2_.Name                                                                          as Name7_0_,
       qualificat2_.QualificationAreaTypeId                                                       as Qualific2_7_0_,
       qualificat2_.QualificationAreaPermissionId                                                 as Qualific4_7_0_,
       qualificat2_.Description                                                                   as Descript5_7_0_,
       qualificat2_.QualificationAreaExpirySettingId                                              as Qualific6_7_0_,
       qualificat2_.DisplayOrder                                                                  as DisplayO7_7_0_,
       qualificat2_.DateCreated                                                                   as DateCrea8_7_0_,
       qualificat2_.DateUpdated                                                                   as DateUpda9_7_0_,
       qualificat2_.ShowOnSignupForm1                                                             as ShowOnS10_7_0_,
       qualificat2_.ShowOnSignupForm2                                                             as ShowOnS11_7_0_,
       qualificat2_.ShowOnSignupForm3                                                             as ShowOnS12_7_0_,
       qualificat2_.AgencyId                                                                      as AgencyId7_0_,
       qualificat3_.QualificationLevelId                                                          as Qualific1_47_1_,
       qualificat3_.Name                                                                          as Name47_1_,
       qualificat3_.Description                                                                   as Descript3_47_1_,
       qualificat3_.DateCreated                                                                   as DateCrea4_47_1_,
       qualificat3_.DateUpdated                                                                   as DateUpda5_47_1_,
       qualificat3_.AgencyId                                                                      as AgencyId47_1_,
       dbo.IsQualificationLevelAssociatedWithAnyQualifications(qualificat3_.QualificationLevelId) as formula21_1_
FROM   Qualification this_
       inner join QualificationArea qualificat2_
         on this_.QualificationAreaId = qualificat2_.QualificationAreaId
       inner join QualificationLevel qualificat3_
         on this_.QualificationLevelId = qualificat3_.QualificationLevelId
WHERE  this_.QualificationAreaId in (1 /* @p0 */,2 /* @p1 */,3 /* @p2 */)
       and this_.QualificationLevelId in (1 /* @p3 */,2 /* @p4 */,3 /* @p5 */)

对我来说,这个逻辑似乎有缺陷,它正在为每个复合键ID执行单独的“In”查询;这会不会返回不正确的结果?

作为参考,我在我的密钥类上正确实现了.Equals和.GetHashCode,所以我确定这不是问题。

1 个答案:

答案 0 :(得分:1)

是的,它会返回错误的结果,定义传递的复合id值的任何对都可以求值为true,所以当你请求例如[[1,1],[2,2],[3,3]时]此查询还将获取[[1,2],[1,3],[2,1] [2,3],[3,1],[3,2]等] ..

我现在能想到的唯一解决方案是对的分离......例如

(this_.QualificationAreaId = 1 AND this_.QualificationLevelId = 1)或

(this_.QualificationAreaId = 2 AND this_.QualificationLevelId = 2)或

(this_.QualificationAreaId = 3 AND this_.QualificationLevelId = 3)

等...