为OneToMany关系构造JPA查询

为OneToMany关系构造JPA查询

本文介绍了为OneToMany关系构造JPA查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个实体

Class A {
    @OneToMany(mappedBy="a")
    private List<B> bs;
}

Class B {

    @ManyToOne
    private A a;

    private String name;
}

1)我想构造一个查询,说得到名称至少为"mohamede1945"的至少有一个B的所有A.

1) I would like to construct a query that says get all A's that have at least one B with name ="mohamede1945"

2)我想构造一个查询,说得到名称不等于"mohamede1945"的所有B都没有的B.

2) I would like to construct a query that says get all A's that don't have any B with name = "mohamede1945"

有人可以帮我吗?

推荐答案

您可以使用ANY和ALL构造来过滤子查询.所以像

You can use the ANY and ALL constructs to filter the subquery. So something like

1. FROM A aEntity WHERE 'mohamede1945' = ANY (SELECT bEntity.name FROM aEntity.bs bEntity)

2. FROM A aEntity WHERE 'mohamede1945' <> ALL (SELECT bEntity.name FROM aEntity.bs bEntity)

这篇关于为OneToMany关系构造JPA查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:17