0

2 つのエンティティ クラスGroupPersonがあり、 と の間Groupに1 対多のPerson関係がある場合 (つまり、個人は最大 1 つのグループに属することができ、グループは多くの個人を持つことができます)、どの jpql/hql クエリを使用できますか?にPersonないすべての を選択するために使用しますGroupか?

の逆のようなものselect p from Group g inner join g.people p...

4

2 に答える 2

1

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.

于 2012-05-16T13:16:08.840 に答える