【问题标题】:SpringBoot JPQL query List NotIn ListSpring Boot JPQL 查询列表不在列表中
【发布时间】:2018-10-18 15:31:48
【问题描述】:

我正在为 MySQL 数据库查询而苦苦挣扎,希望您能帮助我。 这个例子是抽象的,因为问题是查询:

POJO:

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

至少可以通过以下方式过滤 Child like:

List<Parent> findByChildrenNot(Child child);

或类似的东西。

这似乎很容易,但我没有找到解决方案。希望你能帮助我。

提前致谢!

亲切的问候

格雷戈

【问题讨论】:

  • JPQL 中没有!=。它是&lt;&gt;,你不能在列表上做(不)平等!

标签: hibernate spring-boot jpa spring-data-jpa jpql


【解决方案1】:

这应该可以工作(未经测试 - 请提供反馈):

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

更多信息:123

【讨论】:

    【解决方案2】:

    当一个孩子就够了时,像这样使用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); 
    

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 2021-08-22
      • 2017-10-10
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2021-08-21
      相关资源
      最近更新 更多