我猜我有两个具有双向@OneToMany
和@ManyToOne
关联的实体:
CCD_ 3-具有许多->Child
Child
具有boolean
属性x
。
我想选择Parent
实体,其中所有关联的Child
实体都将x
设置为true
例如,如果我有以下数据:
父级:
+-------+
| ID |
+-------+
| 1 |
+-------+
| 2 |
+-------+
儿童:
+-------+-------+-------+
| ID | P.ID | x |
+-------+-------+-------+
| 1 | 1 | true |
+-------+-------+-------+
| 2 | 1 | true |
+-------+-------+-------+
| 3 | 2 | true |
+-------+-------+-------+
| 4 | 2 | false |
+-------+-------+-------+
我想要一个HQL或JPQL查询,它返回具有id
1的Parent
实体。
知道吗?
您可以执行以下查询:
from parent p where p.ID in (select c.parent from child c where c.x=true) and p.ID not in (select c.parent from child c where c.x = false)
您可以像这样使用jpql
查询
select p from Parent p where p.children is not empty and not exists(
select 1 from p.children c where c.x=false)
实体:
@Entity
public class Parent {
//...
@OneToMany
List<Child> children;
//...
}