SpringBoot JPQL query List NotIn List



我在MySQL数据库查询中挣扎,希望你能帮助我。 该示例很抽象,因为问题是查询:

波约:

class Parent
{
List<Child> children;
}
class Child
{
Integer id;
}

现在我想找到所有没有某些孩子的父母。

喜欢:

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); 

可选,至少可以按孩子过滤,例如:

List<Parent> findByChildrenNot(Child 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); 

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

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

最新更新