Spring Data Rest:“日期为空”查询引发 Postgres 异常

问题描述 投票:0回答:5

我使用 Spring Boot 和 Data Rest 在 Java8 中创建一个简单的微服务并获得 postgres 异常。

我的实体:

@Entity
public class ArchivedInvoice implements Serializable {
    ...
    @Column
    private String invoiceNumber;
    @Column
    private java.sql.Date invoiceDate;
    ...
}

我的存储库界面:

@RepositoryRestResource(collectionResourceRel = "archivedinvoices", path = "archivedinvoices")
public interface ArchivedInvoiceRepository extends PagingAndSortingRepository < ArchivedInvoice, Long > {
    ...
@RestResource(rel = "findByXYZ", path = "findByXYZ")
@Query(value = "SELECT ai FROM #{#entityName} ai WHERE "
        + "(:invoiceNumber IS NULL OR ai.invoiceNumber LIKE :invoiceNumber) AND "
        + "(:invoiceDate IS NULL OR ai.invoiceDate = :invoiceDate)" 
        )
public Page < ArchivedInvoice > findByXYZ(
        @Param("invoiceNumber") @Nullable String invoiceNumber,
        @Param("invoiceDate") @Nullable Date invoiceDate,
        Pageable p);
    ...
}

如果我调用“.../findByXYZ?invoiceDate=2016-02-22”,我会收到以下错误消息:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
...
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
...
Caused by: org.postgresql.util.PSQLException: FEHLER: could not determine data type of parameter

但是当我删除“:invoiceDate IS NULL”部分时,它起作用了。如何检查发票日期参数是否为空?

hibernate postgresql jpa spring-data spring-data-rest
5个回答
13
投票

我花了一些时间研究这个,因为我真的想比较查询中的“日期为空”,这就是结果:

当您使用“cast(foo.date as date) is null”时,如果字段不为空,则可以正常工作。如果该字段为空,则会抛出此异常:

org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to date

解决方案是使用合并:

coalesce(:date, null) is null

无论有或没有现场测试数据,它都可以正常工作。

我的查询示例:

@Query("SELECT c "
        + " FROM Entity c "
        + " WHERE "
        + "       and ( (coalesce(:dateFrom, null) is null and coalesce(:dateUntil, null) is null) "
        + "             or ((coalesce(c.date, null) is not null) "
        + "                 and ( "
        + "                        ((coalesce(:dateFrom, null) is null and coalesce(:dateUntil, null) is not null) and c.date <= :dateUntil) "
        + "                     or ((coalesce(:dateUntil, null) is null and coalesce(:dateFrom, null) is not null) and c.date >= :dateFrom)"
        + "                     or (c.date between :dateFrom and :dateUntil)"
        + "                 )"
        + "             )"
        + "       ) "

希望它对你有用!


8
投票

我相信@Bonifacio是正确的,因为Postgres在执行

@Param("invoiceDate")
测试时无法确定你的
IS NULL
参数的类型。我有与您类似的查询,它们在内存 H2 数据库中的行为符合预期,但在 Postgres 集成测试中失败。

我可以通过将参数转换为日期来解决这个问题,如下所示:

@Query(value = "SELECT ai FROM #{#entityName} ai WHERE "
    + "(:invoiceNumber IS NULL OR ai.invoiceNumber LIKE :invoiceNumber) AND "
    + "(cast(:invoiceDate as date) IS NULL OR ai.invoiceDate = :invoiceDate)" 
    )

2
投票

在 postgres 9.5 中,我对 LocalDateTime 类型的字段和 Timestamp 字段也有类似的问题。 我解决了它,像这样转换它:

    @Query("SELECT c "
        + "FROM Contract c "
        + "WHERE "
        + "(:idContract IS NULL OR c.idContract = :idContract) AND "
        + "(CAST(:dtCancelInitial AS java.time.LocalDateTime) IS NULL OR (c.dtCancel >= :dtCancelInitial AND c.dtCancel < :dtCancelFinal)) "
        + "ORDER BY c.idContract")
List<Contract> findContratConsultaCancelamento(@Param("idContract") Long idContract, @Param("dtCancelInitial") LocalDateTime dtCancelInitial, @Param("dtCancelFinal") LocalDateTime dtCancelFinal);

1
投票

当您将 JPA 与 Postgres 一起使用时,会出现一种奇怪的行为,当您想在稍后检查所需条件之前检查参数是否为空时,会导致错误。

发生错误的原因是,在读取查询时,JPA 无法识别参数类型是什么,然后在解释查询时引发错误。

解决方法是切换参数条件的位置,首先检查所需的条件是否为真,然后检查参数是否为空。

这样,JPA 将能够识别正确的参数类型,并且不会再次引发错误。

尝试以下查询,看看它是否有效:

@Query(value = "SELECT ai FROM #{#entityName} ai WHERE "
    + "(:invoiceNumber IS NULL OR ai.invoiceNumber LIKE :invoiceNumber) AND "
    + "(ai.invoiceDate = :invoiceDate OR :invoiceDate IS NULL)" 
    )

0
投票

以下对我有用

@Query(value = "SELECT ai FROM #{#entityName} ai WHERE "
        + "(:invoiceNumber IS NULL OR ai.invoiceNumber LIKE :invoiceNumber) AND "
        + "(cast(:invoiceDate as date) IS NULL OR ai.invoiceDate = :invoiceDate)" 
        )
© www.soinside.com 2019 - 2024. All rights reserved.