简体   繁体   English

JPQL-如何从其他实体集中选择任何行未引用的所有实体

[英]JPQL - how to select all entities not referred to by any row from other entity set

I have entity Parent and Child with unilateral relation Child n -> 1 Parent . 我有实体ParentChild具有单边关系Child n -> 1 Parent In other words Parent does not know about its children. 换句话说, Parent不了解其子级。

How to write an JPQL query which would select all instances of Parent which are not referred to by a particular subset of Child which meet certain predicate? 如何编写JPQL查询,以选择满足特定谓词的Child的特定子集未引用的Parent所有实例?

Example: select all parents which are not referred to by children who are younger than 8 years 示例: select all parents which are not referred to by children who are younger than 8 years

You can try a LEFT OUTER JOIN on the parent/child relationship, eg: 您可以在父子关系上尝试LEFT OUTER JOIN,例如:

SELECT c, p.name FROM Country c LEFT OUTER JOIN c.capital p

If the nulls are a problem, you can get rid of them by adding something like: 如果空值是一个问题,则可以通过添加以下内容来消除它们:

... WHERE p.id IS NOT NULL

For further detailed explanation, see this guide: http://www.objectdb.com/java/jpa/query/jpql/from#LEFT_OUTER_JOIN_ 有关更多详细说明,请参见本指南: http : //www.objectdb.com/java/jpa/query/jpql/from#LEFT_OUTER_JOIN_

If the parent does not know about the children, then you can achieve similar results by using a subquery to filter parent, eg: 如果父级不了解子级,则可以通过使用子查询来过滤父级来获得相似的结果,例如:

SELECT e.parent_id
    FROM ParentEntity e
    WHERE e.parent_id NOT IN (
    SELECT c.parent_id FROM ChildEntity c) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM