从 MySQL 切换到PostgreSQL 后,我发现我的 SQL 查询(spring 数据存储库界面中的 @Query)不再起作用。该问题是由作为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.
Run Code Online (Sandbox Code Playgroud)
带有@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);
}
Run Code Online (Sandbox Code Playgroud)
简单测试:
LOGGER.warn("test1: {}", wineRepository.simpleTest(1L)); //ok
LOGGER.warn("test2: …
Run Code Online (Sandbox Code Playgroud) 如果在代码片段后entity.getHistory()为null:
(getEntityManager()返回弹簧注入的EntityManager,数据库字段历史类型为:text或varchar2(2000)
Query query = getEntityManager().createNativeQuery("insert into table_name(..., history, ....) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
[...]
.setParameter(6, entity.getHistory())
[...]
query.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
给出奇怪的例外:
17/11/11 06:26:09:009 [pool-2-thread-1] WARN util.JDBCExceptionReporter:100 - SQL Error: 0, SQLState: 42804
17/11/11 06:26:09:009 [pool-2-thread-1] ERROR util.JDBCExceptionReporter:101 - ERROR: **column "history" is of type text but expression is of type bytea**
Run Code Online (Sandbox Code Playgroud)
提示:您需要重写或转换表达式.
仅在此配置中出现问题:
操作系统:CentOS版本5.6(最终版)
Java:1.6.0_26
DB:PostgreSQL 8.1
JDBC驱动程序:postgresql-9.1-901.jdbc4
应用程序服务器:apache-tomcat-6.0.28
在其他一些配置或历史记录为空字符串时,一切正常.从pgAdmin执行的相同语句工作正常.
我想问题是在PostgreSQL JDBC驱动程序中,是否有一些明智的理由将空字符串视为bytea值?Postgres 8和9之间可能有一些奇怪的变化?