NHibernate:条件 - 获取子 ID 列表
我有几个处于多对多关系的实体。 给定父 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)]
);
I have a couple of entities in a many to many relationship.
Given the parent ID, how do I, using ICriteria, retrieve a list of children?
Code so far:
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));
However, the SQL generated is:
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 ...);
How do I get it to generate the following?
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)]
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单,直接对孩子查询。
easy, query on the child directly.