Spring Boot JPQL 查询列表不在列表中

SpringBoot JPQL query List NotIn List

我正在为 MySQL 数据库查询而苦恼,希望您能帮助我。 该示例是抽象的,因为问题是查询:

POJO:

class Parent
{
 List<Child> children;
}

class Child
{
 Integer id;
}

现在我想找到所有 Parents 没有特定 children 的。

喜欢:

List<Parent> findByChildrenNotIn(List<Child> childs);

@Query("SELECT p FROM Parent p "
        + "LEFT OUTER JOIN p.children c "
        + "WHERE c.id != ?1 " 
        + "GROUP BY p.id "
        )
List<Parent> findByNotChildren(List<Integer> childIds); 

可以选择至少按 Child 进行过滤,例如:

List<Parent> findByChildrenNot(Child child);

或类似的东西。

看起来很容易,但我没有找到解决方案。希望你能帮助我。

提前致谢!

亲切的问候

格雷戈尔

这应该有效(未经测试 - 请提供反馈):

List<Parent> findDistinctByChildrenIdNotIn(List<Integer> childIds);

@Query("select distinct p from Parent p left join p.children c where c.id not in ?1")
List<Parent> findParents(List<Integer> childIds); 

更多信息:1, 2, 3

当一个 child 足够时,像这样使用 MEMBER OF

@Query("select p from Parent p where :child NOT MEMBER OF p.childs")
List<Parent> findParents(@Param("child")Child child); 

如果你有双向关系,你可以这样查询:

@Query("SELECT DISTINCT c.parent FROM Child c WHERE c NOT IN (:childs)")
List<Parent> findParents(@Param("childs")List<Child> childs);