从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