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)]
);
easy, query on the child directly.
IList<Driver> driverList = Drivers.CreateCriteria()
.Add(Restrictions.Eq("Group.Id", groupId)) <-- modify according to your domain mapping
.SetProjection(Projections.Property("Id"))
.SetResultTransformer(Transformers.DistinctRootEntity)
.List<Driver>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.