2 つのエンティティ クラスGroup
とPerson
があり、 と の間Group
に1 対多のPerson
関係がある場合 (つまり、個人は最大 1 つのグループに属することができ、グループは多くの個人を持つことができます)、どの jpql/hql クエリを使用できますか?にPerson
ないすべての を選択するために使用しますGroup
か?
の逆のようなものselect p from Group g inner join g.people p
...
If you want to have all persons who are not in a given group g, it should be like
from Person p where p.group.id != :gid;
and you set gid to the id of the given group.
If you want to have all persons who are in no group at all
from Person p where p.group.id is null;
If you want to have all persons who are in no group at al, but it is possible the foreign key in Person has a group id which is not null but does not belong to an existing group (for example if the group was deleted without deleting the persons in it and without moving them to a different group)
from Person p where not exist (select 1 from Group g where g.id = p.group.id);
P. S. My statements are for HQL, but for JPQL it should be more or less the same.