gpt4 book ai didi

sql - Hibernate的PostgreSQL `could not determine data type of parameter`

转载 作者:行者123 更新时间:2023-12-03 13:51:01 33 4
gpt4 key购买 nike

我有 JpaRepository :

@Repository
public interface CardReaderRepository extends JpaRepository<CardReaderEntity, Integer > {


}

当我在此仓库中执行查询时与此相同:
@Query(
value = "SELECT new ir.server.component.panel.response." +
"InvalidCardReaderDataDigestResponse(" +
" cr.driverNumber, cr.exploiterCode, cr.lineCode, " +
" cr.transactionDate, cr.offLoadingDate, " +
" cr.cardReaderNumber, " +
" sum(cr.count) , sum(cr.remind) " +
") " +
"FROM CardReaderEntity cr " +
"WHERE cr.company.id = :companyId " +
"AND cr.status.id in :statusIds " +
"AND cr.deleted = false " +
"AND (:fromDate is null or cr.offLoadingDate >= :fromDate ) " +
"AND (:toDate is null or cr.offLoadingDate <= :toDate ) " +
"group by cr.driverNumber, cr.exploiterCode, cr.lineCode, cr.transactionDate, cr.offLoadingDate, cr.cardReaderNumber"
)
Page<InvalidCardReaderDataDigestResponse> findAllInvalidCardReaderDataDigestByCompanyIdAndStatusIdIn(
@Param( "companyId" ) int companyId,
@Param( "fromDate" ) Date fromDate,
@Param( "toDate" ) Date toDate,
@Param( "statusIds" ) List<Integer> statusIds,
Pageable pageable
);

错误是:
org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $3
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178) ~[postgresql-42.2.2.jar:42.2.2]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306) ~[postgresql-42.2.2.jar:42.2.2]

但是当更改查询到:
@Query(
value = "SELECT new ir.server.component.panel.response." +
"InvalidCardReaderDataDigestResponse(" +
" cr.driverNumber, cr.exploiterCode, cr.lineCode, " +
" cr.transactionDate, cr.offLoadingDate, " +
" cr.cardReaderNumber, " +
" sum(cr.count) , sum(cr.remind) " +
") " +
"FROM CardReaderEntity cr " +
"WHERE cr.company.id = :companyId " +
"AND cr.status.id in :statusIds " +
"AND cr.deleted = false " +
"AND (:fromDate is null ) " +
"AND (:toDate is null ) " +
"group by cr.driverNumber, cr.exploiterCode, cr.lineCode, cr.transactionDate, cr.offLoadingDate, cr.cardReaderNumber"
)
Page<InvalidCardReaderDataDigestResponse> findAllInvalidCardReaderDataDigestByCompanyIdAndStatusIdIn(
@Param( "companyId" ) int companyId,
@Param( "fromDate" ) Date fromDate,
@Param( "toDate" ) Date toDate,
@Param( "statusIds" ) List<Integer> statusIds,
Pageable pageable
);

这项工作没有错误,但是希望使用从日期和 到日期运行

笔记:

CardReaderEntity 中的 offLoadingDate :
@Basic
@Temporal( TemporalType.TIMESTAMP )
public Date getOffLoadingDate() {
return offLoadingDate;
}
public void setOffLoadingDate(Date offLoadingDate) {
this.offLoadingDate = offLoadingDate;
}

我的Java文件中所有Date导入都是 java.util.Date

最佳答案

PostgreSQL驱动程序尝试找出参数的类型,以将这些参数直接告知PostgreSQL服务器。 PostgreSQL服务器必须能够比较字段,这是必要的。如果使用java.sql.Timestamp,则PostgreSQL驱动程序将无法执行该操作,因为PostgreSQL有两个匹配的字段。一方面是时间戳,另一方面是带有时区信息的时间戳。结果是,PostgreSQL驱动程序将其与Oid.UNSPECIFIED匹配。在大多数情况下,此行为不是问题,因为PostgreSQL服务器能够检测到该类型。 PostgreSQL驱动程序类PgPreparedStatement中对此问题进行了详细描述。
因此,仅在Postgres无法检测到正确的类型时(当您检查null时),您才可以强制将其强制转换为时间戳/日期类型。
所以代替

(:fromDate is null )
(cast(:fromDate as date) is null )
与toDate相同

关于sql - Hibernate的PostgreSQL `could not determine data type of parameter`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089400/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com