繁体   English   中英

Spring 数据 jpa PostgreSql:运算符不存在:没有时区的时间戳 >= bytea

[英]Spring data jpa PostgreSql: operator does not exist: timestamp without time zone >= bytea

使用 Spring 启动 2.6.6、Spring 数据 JPA、Hibernate 5.6.7.Final、PostgreSql 驱动程序 42.3.3、PostgreSql 服务器 14。

我有查询: SELECT u.* FROM "user" u WHERE ((:createdAtFrom = NULL OR:createdAtTo = NULL) OR (u.birthday BETWEEN:createdAtFrom AND:createdAtTo))

但它不起作用。

我收到错误:

org.postgresql.util.PSQLException: ERROR: operator does not exist: timestamp without time zone >= bytea
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

我为 sql 参数打开 hibernate 调试并查看下一行:

o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARBINARY] - [null]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARBINARY] - [null]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARBINARY] - [null]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARBINARY] - [null]

为什么是VARBINARY 我试过java.util.Datejava.time.LocalDateTime - 同样的错误。 怎么了?

有演示回购: https://gitlab.com/Tsyklop/jpa-test/-/tree/master

您的语句似乎缺少CAST ,因此 Postgresql 知道绑定参数是它们所比较的列的类型。

此外,与NULL的比较应始终与IS NULL 请参阅为什么 SQL 不支持“= null”而不是“is null”?

所以像

SELECT u.* FROM "user" u 
WHERE ((:createdAtFrom IS NULL OR :createdAtTo IS NULL) 
OR (u.birthday BETWEEN 
    CAST (:createdAtFrom TO TIMESTAMP) 
    AND CAST (:createdAtTo TO TIMESTAMP))
)

应该管用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM