为 OneToMany 关系构建 JPA 查询
我有这 2 个实体
Class A {
@OneToMany(mappedBy="a")
private List<B> bs;
}
Class B {
@ManyToOne
private A a;
private String name;
}
1) 我想构造一个查询,表示获取所有至少有一个名称为“mohamede1945”的 B 的 A
2) 我想构造一个查询,表示获取所有没有名称的 A有任何 B 的名字=“mohamede1945”
有人可以帮助我吗?
I've those 2 entities
Class A {
@OneToMany(mappedBy="a")
private List<B> bs;
}
Class B {
@ManyToOne
private A a;
private String name;
}
1) I would like to construct a query that says get all A's that have at least one B with name ="mohamede1945"
2) I would like to construct a query that says get all A's that don't have any B with name = "mohamede1945"
Could anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您可以通过查看此链接并搜索 JOIN 来了解答案:http://download.oracle.com/docs/cd/E11035_01/kodo41/full/html/ejb3_langref.html
第二个所有,这是我的方法:
在您的 DAO 中,您可以像这样创建命名查询:
First of all, I think you can learn the answer by looking at this link and search for JOIN: http://download.oracle.com/docs/cd/E11035_01/kodo41/full/html/ejb3_langref.html
Second of all, here is my approach:
In you DAO, you can make the namedquery like this:
您可以使用 ANY 和 ALL 结构来过滤子查询。所以像
You can use the ANY and ALL constructs to filter the subquery. So something like