我正在尝试编写一个Hibernate(JPA)查询,以选择那些 ALL 其子项匹配属性的父项。
让我们做一个实际的例子......
我想选择那些 ALL 他们的孩子金发碧眼的父亲。如果只有一个是黑头发的父亲没有被选中。
查询将如何? 提前谢谢!
答案 0 :(得分:4)
也许这样的事情会起作用:
From Father f
where not exists (select c from f.children c where not c.hair = "BLONDE");
只是一个想法......
答案 1 :(得分:1)
使用ALL
尝试此操作
见http://openjpa.apache.org/builds/1.1.0/docs/jpa_langref.html#jpa_langref_all_any:
select p from parent where 'blonde'=all(parent.children.haircolor)
答案 2 :(得分:1)
这也应该有效,看起来更清洁。
SELECT p from Parent p join p.children c where c.haircolor = 'blonde';