简体   繁体   中英

JPQL group by having count(*)

I have a strange problem with my JQPL queries. 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. But why does it expect a Long?

Does anybody have a clue?

Thanks!

UPDATE WITH SOLUTION:

As JB correctly commented the following now works:

@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 .

The error message explains it. The query expects a Long, but you're passing an Integer. Change the signature to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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