Not planned
Description
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))
Native.
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
Stackoverflow: https://stackoverflow.com/questions/71902768/spring-boot-2-postgresql-operator-does-not-exist-timestamp-without-time-zone
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
Tsyklop commentedon Apr 17, 2022
I tried one thing. And if pass
null
there I got error.Is there any workarounds? I want search all rows without timestamp filter or with timestamp filter.
gregturn commentedon Apr 18, 2022
What happens if you use
IS NULL
instead of= NULL
?If this is native SQL via
@Query(value="/* SQL */", nativeQuery=true)
, thennull
doesn't equalnull
and requires a specificIS NULL
check to boolean short circuit out of thatWHERE
clause.Tsyklop commentedon Apr 18, 2022
I tried
IS NULL
and= NULL
- same error.You can change query in project and check this.
Error gone when I pass objects of Instant class instead of null.
schauder commentedon Apr 19, 2022
@gregturn is correct about
= NULL
vsIS NULL
but that is a different problem unrelated to the exception.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. So something likeSELECT 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.Could you confirm, that this works?
Tsyklop commentedon Apr 19, 2022
I have next query:
And it transforms to this query:
And I got an error, because in
order by
incorrect entry:order by NULL.id desc limit ?
. Why?I Used
Pageable
as a parameter for Repository.https://gitlab.com/Tsyklop/jpa-test/-/blob/master/src/main/java/com/example/jpatest/persistance/UserRepository.java#L46
Tsyklop commentedon Apr 19, 2022
I created query without
Pageable
:https://gitlab.com/Tsyklop/jpa-test/-/blob/master/src/main/java/com/example/jpatest/persistance/UserRepository.java#L30
I passed
null
tocreateAt
parameters and got error:org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to timestamp without time zone
.Query Log:
22 remaining items