选择实体包含作为另一个列表子集的列表的位置

发布于 2024-10-11 09:26:30 字数 158 浏览 1 评论 0原文

我正在编写 JPQL 查询,并且有以下场景。我有一个包含标签列表的问题实体。我想选择包含给定标签列表的所有问题。我如何使用 JPA 执行此操作?

我想做一些类似 SELECT x FROM Question x WHERE x.tags 'contains all' :tags 的事情

I am writing a JPQL query and i have the following scenario. I have a Question entity which contains a list of Tags. I would like to select all Questions that contains a given List of tags. How do i do this with JPA?

I would like to do something like SELECT x FROM Question x WHERE x.tags 'contains all' :tags

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

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

发布评论

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

评论(3

国际总奸 2024-10-18 09:26:30

尝试这样:

select distinct q from Question q join q.tags as t 
where t.name in (:tags) 
group by q.id, q.author, q.title, q.content,q.postedAt 
having count(t.id) = :size

Try like this:

select distinct q from Question q join q.tags as t 
where t.name in (:tags) 
group by q.id, q.author, q.title, q.content,q.postedAt 
having count(t.id) = :size
她如夕阳 2024-10-18 09:26:30

Nayans 解决方案对我不起作用。它选择与给定集合“:tags”的第一个(或任何?)条目匹配的每个“x”。如果这确实对您有用,您应该再次测试您的应用程序;)可能是 JPA 依赖项 - 我不知道。

提示:尝试 Krzysztofs 解决方案或使用我的:

SELECT x FROM Question x 
WHERE x IN (
    SELECT y FROM Question y
    INNER JOIN y.tags yt
    WHERE yt IN (
        :tags
    )
    GROUP BY y
    HAVING COUNT( DISTINCT yt) = (
        :tagsSize // should be clear ;)
    )
)

Nayans solution does'nt work for me. Its selecting every 'x' which matches the first (or any?) entry of the given collection ':tags'. If this really worked for you, you should test you application again ;) might be JPA dependend - I don't know.

Tip: Try Krzysztofs solution or use mine:

SELECT x FROM Question x 
WHERE x IN (
    SELECT y FROM Question y
    INNER JOIN y.tags yt
    WHERE yt IN (
        :tags
    )
    GROUP BY y
    HAVING COUNT( DISTINCT yt) = (
        :tagsSize // should be clear ;)
    )
)
假情假意假温柔 2024-10-18 09:26:30

[这搜索任何而不是全部;请参考其他正确答案。]

您可以将 list 设置为参数。

SELECT x FROM Question x WHERE x.tags IN :tags

还可以尝试使用 (:tags),因为它取决于您正在使用的 JPA 实现。

[This searches for ANY not ALL; please refer other correct answers.]

You can set list as a parameter.

SELECT x FROM Question x WHERE x.tags IN :tags

Also try using (:tags), as it depends on the JPA implementation you are using.

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