JPA JPQL: select items when attribute of item (list/set) contains another item

fea17e86 picture fea17e86 · Dec 1, 2011 · Viewed 36.3k times · Source
public class Document extends Model {
... 
@ManyToMany
public Set<User> accessors;
...
}

I want to select all Documents which accessors contain a certain user. I have just minimal experiences with SQL and no experiences with JPQL. So how to do that?

thanks in advance

Answer

rhlobo picture rhlobo · Sep 28, 2012
SELECT d FROM Document AS d WHERE :user MEMBER OF d.accessors

Should be what you need, and it is simpler than joining tables. Just dont forget to use the user as a parameter instead of using its id:

query.setParameter("user", user);