NHibernate:条件 - 获取子 ID 列表

发布于 2024-10-29 11:19:44 字数 1775 浏览 5 评论 0原文

我有几个处于多对多关系的实体。 给定父 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2024-11-05 11:19:44

简单,直接对孩子查询。

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

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文