我在多对多关系中有几个实体。给定父 ID,我如何使用 ICriteria 检索子列表?
到目前为止的代码:
var driverList = DriverGroup.CreateCriteria()
.Add(Restrictions.IdEq(groupId))
.SetFetchMode("Drivers", FetchMode.Eager)
.SetResultTransformer(Transformers.DistinctRootEntity)
.UniqueResult<DriverGroup>().Drivers.Select(x => x.Id).
ToArray();
var criteria = Driver.CreateCriteria()
.Add(Restrictions.In("Id", driverList));
但是,生成的 SQL 是:
SELECT
this_.groupId as groupId5_1_,
this_.name as name5_1_,
this_.type as type5_1_,
drivers2_.groupId as groupId3_,
... fields snipped ...
drivers3_.userId as ID3,
... fields snipped ...
FROM
dbo.Groups this_
left outer join
dbo.Object_Rel drivers2_
on this_.groupId=drivers2_.groupId
left outer join
dbo.vwDriverSimple driverbase3_
on drivers2_.ID=driverbase3_.userID
WHERE
this_.type=0 AND this_.groupId = @p0;
@p0 = 443 [Type: Int32 (0)]
SELECT
this_.userID as userID10_1_,
... fields snipped ...
FROM
dbo.drivers this_
WHERE
this_.userID in (
@p0, ... lots of parameters ...);
如何让它生成以下内容?
SELECT
this_.userID as userID10_1_,
... fields snipped ...
FROM
dbo.drivers this_
WHERE
this_.userID in (
SELECT
drivers3_.userId as ID3
FROM
dbo.Groups this_
left outer join
dbo.Object_Rel drivers2_
on this_.groupId=drivers2_.groupId
left outer join
dbo.vwDriverSimple driverbase3_
on drivers2_.ID=driverbase3_.userID
WHERE
this_.type=0 AND this_.groupId = @p0;
@p0 = 443 [Type: Int32 (0)]
);