【问题标题】:Can I Query Spring JPA @Query for any match of a list with another list?我可以查询 Spring JPA @Query 以获取列表与另一个列表的任何匹配吗?
【发布时间】:2017-02-28 12:03:47
【问题描述】:

我需要找到父实体中包含列表的匹配实体,我将传入 Id 列表作为参数,并且我想匹配内部列表包含任何参数 Id 的任何父实体,可以这可以在没有谓词方法的情况下完成吗? 我知道以下内容行不通,但会感谢任何指针,已阅读 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ 并且未涵盖此场景。

@Query("SELECT CASE WHERE COUNT (parent) > 0 THEN 'true' ELSE 'false' END FROM Parent parent LEFT JOIN parent.children children where :ids in children")
boolean isFound(@Param("ids") List<Long> ids;

【问题讨论】:

    标签: java spring hibernate jpa


    【解决方案1】:

    如果您使用 Spring JPA,您可以执行以下操作:

    @Repository
    public interface ParentRepository extends CrudRepository<Parent, Long> {
        Boolean existsCountByChildrenIn(List<Integer> ids);
    }
    

    【讨论】:

    • 嗨,谢谢,我实际上不得不做一个@Query,因为逻辑要复杂得多,但让我感到困惑的是列表问题中的列表。然而,你的回答让我意识到我已经把它复杂化了,当我对孩子们进行左连接时,每个 child.id 都是一个单一的 id,我可以很高兴地为此做一个“in :ids”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 2018-01-19
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多