Selecting where an entity contains a list thats a subset of another list

joshua picture joshua · Jan 9, 2011 · Viewed 14k times · Source

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

Answer

Krzysztof Kaczmarek picture Krzysztof Kaczmarek · Feb 26, 2012

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