HQL 对子集合的限制

发布于 2024-09-26 04:03:36 字数 307 浏览 0 评论 0原文

我有一个类,它有一个集合

public class Parent
{
    ...
    ISet<Child> Children;
    ...
}

给定一个子名称列表,我想返回其 Children 属性包含该列表的所有项目的父母。

我设法编写了一个 HQL 查询,但它适用于单个名称,而不是整个列表:

SELECT p FROM Parent AS p JOIN p.Children AS c WHERE c.Name = 'MyName'

I have a class which has a collection

public class Parent
{
    ...
    ISet<Child> Children;
    ...
}

Given a list of child names, I'd like to return parents whose Children property contains all items of this list.

I managed to write an HQL query, but it works for a single name, not a whole list :

SELECT p FROM Parent AS p JOIN p.Children AS c WHERE c.Name = 'MyName'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

亣腦蒛氧 2024-10-03 04:03:36

谢谢毛里西奥,我设法通过以下条件查询解决了这个问题

var criteria = session.CreateCriteria<Parent>("p")
                        .Add(Restrictions.IsNotEmpty("p.Children"));

foreach (var name in namesToMatch)
{
     criteria.Add(Subqueries.Exists(DetachedCriteria.For<Parent>("p2")
                            .SetProjection(Projections.Id())
                            .CreateAlias("p2.Children", "c")
                            .Add(Restrictions.Eq("c.Name", name))
                            .Add(Restrictions.EqProperty("p2.Id", "p.Id"))));
}

Thanks Mauricio, I managed to solve this problem with the following criteria query

var criteria = session.CreateCriteria<Parent>("p")
                        .Add(Restrictions.IsNotEmpty("p.Children"));

foreach (var name in namesToMatch)
{
     criteria.Add(Subqueries.Exists(DetachedCriteria.For<Parent>("p2")
                            .SetProjection(Projections.Id())
                            .CreateAlias("p2.Children", "c")
                            .Add(Restrictions.Eq("c.Name", name))
                            .Add(Restrictions.EqProperty("p2.Id", "p.Id"))));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文