【发布时间】:2016-08-18 11:42:27
【问题描述】:
从 MySQL 切换到 PostgreSQL 后,我发现我的 SQL 查询(spring 数据存储库接口中的@Query)不再起作用。问题是由 null 值作为 bytea 发送的,我收到以下异常:
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
在实际情况下,我有多个可以为空的参数,我不希望在 java 代码中检查它们,而是将它们发送到 sql 查询。我在 stackoverflow 上检查了这里的问题,但没有找到一个很好的答案,尤其是对于 spring 数据存储库 @query 注释。
使用 PostgreSQL 处理空值的正确方法是什么?或者您有任何提示如何解决我的方法?谢谢!
更新:
问题似乎与 nativeQuery = true 有关,当值为 false 时,空值按预期工作。所以问题是即使启用了 nativeQuery 是否也可以使其正常工作。
【问题讨论】:
标签: java hibernate postgresql spring-data spring-data-jpa