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

前端 未结 2 1381
花落未央
花落未央 2020-12-15 18:28
public class Document extends Model {
... 
@ManyToMany
public Set accessors;
...
}

I want to select all Documents which accessors conta

相关标签:
2条回答
  • 2020-12-15 18:49
    select distinct d from Document d inner join d.accessors a where a.id = :id
    

    You should learn how SQL joins work, and then learn how to use joins in JPQL. That's essential. You'll find plenty of tutorials online. Google is your friend.

    0 讨论(0)
  • 2020-12-15 18:56
    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);
    
    0 讨论(0)
提交回复
热议问题