简体   繁体   English

JPQL组通过计数(*)

[英]JPQL group by having count(*)

I have a strange problem with my JQPL queries. 我的JQPL查询有一个奇怪的问题。 I got bookmarks and tags, those two have a many to many relationship, set up via a join table. 我有书签和标签,这两个通过联接表建立了多对多关系。 Now I want to query all bookmarks which have all tags. 现在,我要查询具有所有标签的所有书签。

The following works. 以下作品。 It gives me that one bookmark I know it should return. 它使我知道应该返回一个书签。

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

Now I am trying to parameterise this. 现在,我试图对此进行参数化。 I want to pass in the list of tags and the expected count. 我想传递标签列表和预期数量。 And it doesn't work. 而且它不起作用。

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

I get the following exception: 我得到以下异常:

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)]

So the parameter value is correct, as I am passing in the size of the list of tags which is three as in the static example. 所以参数值是正确的,因为我要传递的标签列表的大小是3,如静态示例中那样。 But why does it expect a Long? 但是为什么会期待龙呢?

Does anybody have a clue? 有人知道吗?

Thanks! 谢谢!

UPDATE WITH SOLUTION: 解决方案更新:

As JB correctly commented the following now works: 正如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);

Use java.lang.Long instead of int . 使用java.lang.Long代替int

The error message explains it. 错误消息对此进行了解释。 The query expects a Long, but you're passing an Integer. 查询期望Long,但是您要传递Integer。 Change the signature to 将签名更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM