從MySQL切換到PostgreSQL我發現我的SQL查詢(@Query在spring數據存儲庫接口中)不再有效。該問題是由空值造成的被髮送作爲BYTEA和我得到以下異常:Spring數據存儲庫發送null作爲bytea到PostgreSQL數據庫
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
庫與@Query:
public interface WineRepository extends PagingAndSortingRepository<Wine, Long> {
@Query(value = "SELECT * FROM WINE w WHERE (?1 IS NULL OR w.id = ?1)", nativeQuery = true)
Wine simpleTest(Long id);
}
簡單的測試:
LOGGER.warn("test1: {}", wineRepository.simpleTest(1L)); //ok
LOGGER.warn("test2: {}", wineRepository.simpleTest(null)); //PSQLException
在真實情況下,我有多個參數可以爲null,我不希望在java代碼中檢查它們但將它們發送到SQL查詢。我在這裏檢查了stackoverflow上的問題,但沒有找到一個很好的答案,特別是對於spring數據存儲庫@query註釋。
什麼是用PostgreSQL處理空值的正確方法?或者你有什麼提示如何解決我的方法?謝謝!
更新: 問題似乎與nativeQuery = true
有關,當值爲false時,空值按預期工作。所以問題是,即使啓用了nativeQuery,是否可以使其發揮作用。
但我的意圖是檢查來自Java的輸入是否爲空。我不想從數據庫中選擇空值的值。 – rhorvath