Spring data repository sends null as bytea to PostgreSQL database Spring data repository sends null as bytea to PostgreSQL database postgresql postgresql

Spring data repository sends null as bytea to PostgreSQL database


Try this.

SELECT *FROM WINE wWHERE ?1 IS NULL OR w.id = CAST(CAST(?1 AS TEXT) AS BIGINT)

It satisfies the type checker and should have the same properties as the original query. CAST is not a big performance hit if it happens on a constant value rather than a value from a database row.


You are trying to check whether a Java null is equal to Postgres NULL which I think is not necessary. You can rewrite as following and avoid sending null to the simpleTest function at all.

@Query(value = "SELECT * FROM WINE w WHERE (w.id IS NULL OR w.id = ?1)", nativeQuery = true)Wine simpleTest(Long id);


I had a similar issue in an @Query where a Long was being interpreted by Postgres as a bytea. The solution for me was to unbox the @Param...by passing a long value, Postgres correctly interpreted the value as a bigint