abedurftig
abedurftig

Reputation: 1228

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.

Upvotes: 0

Views: 1396

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

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

Upvotes: 1

Related Questions