当前位置: 首页 > 知识库问答 >
问题:

Spring Data REST:“date is null”查询引发postgres异常

习宸
2023-03-14
@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);
    ...
}
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

共有1个答案

宋斌
2023-03-14

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

当您使用“cast(foo.date as date)is null”时,如果字段不是null,它就可以工作。如果字段为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)"
        + "                 )"
        + "             )"
        + "       ) "
 类似资料:
  • 我正在使用hibernate data JPA。这是我的资源库 公共接口MappingRepository扩展了JpaRepository(ConfigMapping,Long){ 现在在我的服务类中,当我试图通过使用 所以我总是遇到例外 注意:vMList是作为Long列表的VM列表

  • 当我们尝试用Null值获取数据时 IN子句获得空指针异常。 也许是因为这个。 在数据库中,我们可以提供null in IN子句。 jooq中存在一个“无法修复”的问题https://github.com/jOOQ/jOOQ/issues/3867 有一些替代方案: 在输入前检查null(在我的情况下,这是一个非常大的select语句) 所以如果我想让这成为可能,还有其他的解决方法吗。 注:类似的情

  • 问题内容: 我有一个具有以下架构的postgres表 现在,当我运行表单查询时: 我正在制定以下计划: 现在,我无法理解查询计划的执行方式。查询计划是否首先从myTable的索引Designation_place_name检索序列号,然后转到myTable并获取行,然后对timeOfJoining执行过滤 或者 查询计划是否同时获取索引timeOfJoining_timeOfLeaving和Des

  • 问题内容: 我有一个实体和一个实体。关系的定义如下: 现在,当我删除角色时,需要从具有该角色的所有用户中删除该角色。通常,您可以通过查找具有该角色的所有用户,从列表中删除该角色并保存用户来执行类似的操作。但是,当可能有超过一百万的用户时,我不想遍历应用程序中的这么多实体。因此,我想使用本机查询从联接表中删除行。我尝试将其添加到我的存储库中: 但是,当我这样做时,我在日志中看到以下内容: 我不明白在

  • 问题内容: 我正在尝试向表中插入一些行…我正在使用 postgressql-7.2.jar。 我得到以下异常 org.postgresql.util.PSQLException:查询未返回任何结果。 在org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:255) 我已经用Googl

  • 问题内容: 我正在尝试在postgres中的查询中使用group by。我无法按照我想要的方式对其进行工作,以便根据需要对结果进行分组。 这是对我刚刚回答的递归查询的另一个堆栈问题的扩展。但是现在我需要能够将结果分组到最终查询的root_id列上。这是之前的查询: 这是我想做的,以便将具有相同parent_comment_id的所有记录保存在一起。 可能有许多记录返回了相同的parent_comm