【问题标题】:Spring-boot JPA, query with LIKE and NULL fieldsSpring-boot JPA,使用 LIKE 和 NULL 字段查询
【发布时间】:2019-05-29 23:17:09
【问题描述】:

我正在使用 Spring-Boot 1.5.10 和 JPA 连接到 SQL Server 2016。 我确实有一个正在执行 LIKE 的自定义查询,但是当字段为 NULL 时,不返回任何记录:

@Query("SELECT s FROM Speaker s where " +
        "(" +
        "lower(s.lastName) like lower(concat('%', ?1,'%')) " +
        "OR lower(s.firstName) like lower(concat('%', ?1,'%')) " +
        "OR lower(s.biography) like lower(concat('%', ?1,'%'))" +
        ") " +
        "and lower(s.language) like lower(concat('%', ?2,'%')) " +
        "and lower(s.contactType) like lower(concat('%', ?3,'%')) " +
        "and s.fee <= ?4")
Page<Speaker> findByContains(String criteria, String language, String type, int fee, Pageable pageable);

查询连接到一个 REST 控制器,其中传递了一个名为 criteria 的参数,问题是您可能调用 REST 控制器而根本没有传递任何条件。在这种情况下,查询不会返回其中一个搜索条件在数据库中为 NULL 的记录。

【问题讨论】:

    标签: java sql-server spring spring-boot


    【解决方案1】:

    您可以使用 COALESCE 将 null 转换为 emtpy 字符串:

    lower(COALESCE(s.firstName,''))
    

    等等

    【讨论】:

    • 好了,这正是我想要的。问题出在 SQL Server 上,而不是 JPA 查询上
    猜你喜欢
    • 2021-10-13
    • 2021-08-21
    • 1970-01-01
    • 2021-10-18
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多