gpt4 book ai didi

java - JPQL 通过 count(*) 进行分组

转载 作者:行者123 更新时间:2023-12-01 09:44:35 25 4
gpt4 key购买 nike

我的 JQPL 查询有一个奇怪的问题。我有书签和标签,这两者具有多对多关系,通过连接表设置。现在我想查询所有包含所有标签的书签。

以下作品。它给了我一个我知道它应该返回的书签。

@Query("select b from Bookmark b left join b.tags t where t.id in ('mtb', 'video', 'news') group by b.id having count(*) = 3")
Collection<Bookmark> findByTagsStatic();

现在我正在尝试对此进行参数化。我想传递标签列表和预期计数。 它不起作用。

@Query("select b from Bookmark b left join b.tags t where t.id in ?1 group by b.id having count(*) = ?2")
Collection<Bookmark> findByTags(Collection<String> tags, int count);

我收到以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [3] did not match expected type [java.lang.Long (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [3] did not match expected type [java.lang.Long (n/a)]

所以参数值是正确的,因为我传递的标签列表的大小是三个,如静态示例中所示。但为什么它需要 Long 呢?

有人知道吗?

谢谢!

更新解决方案:

正如 JB 正确评论的那样,以下内容现在可以工作:

@Query("select b from Bookmark b left join b.tags t where t.id in ?1 group by b.id having count(*) = ?2")
Collection<Bookmark> findByTags(Collection<String> tags, Long count);

使用java.lang.Long而不是int .

最佳答案

错误消息对此进行了解释。该查询需要一个 Long,但您传递的是一个 Integer。将签名更改为

findByTags(Collection<String> tags, long count);

关于java - JPQL 通过 count(*) 进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com