Search code examples
jpacollectionsjpql

JPA JPQL: select entities with List attribute


I have an entity which have a collection of tags

@ManyToMany
private Set<Tag> listeTag;

I want to write a query which returns a list of my entity which contains all tags passed in parameters and not just one like this.

select distinct entity from Entity entity where and entity.listeTag  in :listeTag

if there are two tags in listeTag I want only entities which have at least the two tags.


Solution

  • select e from SomeEntity e where :numberOfTagsInSet = 
        (select count(tag.id) from SomeEntity e2 
         join e2.listeTag tag 
         where e.id = e2.id 
         and tag.id in :setOfTagIds)
    

    That should do the trick.