list JPA JPQL:当项目(列表/集合)的属性包含另一个项目时选择项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8339889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA JPQL: select items when attribute of item (list/set) contains another item
提问by fea17e86
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?
我想选择访问者包含某个用户的所有文档。我对 SQL 的经验很少,对 JPQL 没有经验。那么怎么做呢?
thanks in advance
提前致谢
回答by rhlobo
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:
应该是你需要的,而且比连接表简单。只是不要忘记使用用户作为参数而不是使用其 id:
query.setParameter("user", user);
回答by JB Nizet
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.
您应该学习 SQL 连接的工作原理,然后学习如何在 JPQL 中使用连接。这是必不可少的。你会在网上找到很多教程。谷歌是你的朋友。