弹簧数据JPA PostgreSQL:操作员不存在:没有时区的时间戳> = bytea

发布于 2025-01-22 02:18:57 字数 1076 浏览 0 评论 0 原文

使用Spring Boot 2.6.6,Spring Data JPA,Hibernate 5.6.7. -Final,PostgreSQL驱动程序42.3.3,PostgreSQL Server 14。

我有查询: 从“用户” u中选择u。

但是它不起作用。

我有错误:

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.date java.time.localdatetime - 相同错误。怎么了?

有演示回购:

Use Spring Boot 2.6.6, Spring data JPA, Hibernate 5.6.7.Final, PostgreSql Driver 42.3.3, PostgreSql Server 14.

I have query:
SELECT u.* FROM "user" u WHERE ((:createdAtFrom = NULL OR :createdAtTo = NULL) OR (u.birthday BETWEEN :createdAtFrom AND :createdAtTo)).

But it not working.

I got error:

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.

I turned on hibernate debug for sql parameters and see next rows:

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]

Why VARBINARY?
I tried java.util.Date, java.time.LocalDateTime - same error. what wrong?

There is demo repo: https://gitlab.com/Tsyklop/jpa-test/-/tree/master

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕烟庭风 2025-01-29 02:18:57

您的语句似乎缺少 cast ,因此PostgreSQL知道绑定参数是与它们相比的列的类型。

同样,与 null 进行比较,应始终使用为null 。参见为什么不sql support sql support = null”而不是” 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))
)

应该工作。

Your statement seems to be missing a CAST so that Postgresql knows that the bind parameters are of the type of the columns they get compared to.

Also comparisons with NULL should always be made with IS NULL. See Why doesn't SQL support "= null" instead of "is null"?

So something like

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))
)

should work.

粉红×色少女 2025-01-29 02:18:57

就我而言:Java(与Joda Time),Hibernate和Postgresql。

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
createdAtTo

为了解决它,只需使用此处所述的实体类中提供的解决方案:Joda Time Hibernate用户指南。
https://www.joda.org/joda time-hibernate/joda time-hibernate/userguide。 html

源代码:github -Joda Time Hibernate
https://github.com/jodaorg/jodaorg/joda time-hibernate
Maven:Joda Time Hibernate在Maven存储库上
https://mvnrepository.com/artifact/artifact/joda time/joda time-time-pime-ima-po.冬眠

In my case: Java (with Joda-Time), Hibernate, and PostgreSQL.

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
createdAtTo

To resolve it, simply use the solution provided in your entity class as described here: Joda-Time Hibernate User Guide.
https://www.joda.org/joda-time-hibernate/userguide.html

Source code: GitHub - Joda-Time Hibernate
https://github.com/JodaOrg/joda-time-hibernate
Maven: Joda-Time Hibernate on Maven Repository
https://mvnrepository.com/artifact/joda-time/joda-time-hibernate

莳間冲淡了誓言ζ 2025-01-29 02:18:57

就我而言,只要我需要使用可能为null的变量,我都会使用 cast(:starttime作为文本)

示例:
and(cast(:启动时间为文本)为null或cb.starttime<:starttime)

In my case, whenever I need to use a variable that might be null, I use cast(:startTime as text)

Example :
AND (cast(:startTime as text) IS NULL OR cb.startTime < :startTime)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文