首页 > 解决方案 > 将 postgres bytea 与字符串进行比较时,Spring JDBC 抛出异常

问题描述

我有一张这样的桌子

create table tabl(id bytea, val text);

&这样的查询

select * from tabl where id='foo';

当我在 PSQL 或 pgAdmin 中执行此查询时,它可以工作

但是当 Spring JdbcTemplate 执行相同的查询时,它会引发异常

exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: bytea = character varying
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

Java 代码如下所示:

jdbcTemplate.queryForObject("select val from tabl where id=?", new Object[] {id}, String.class)

虽然我知道我可以通过使用decode函数来解决这个问题

jdbcTemplate.queryForObject("select val from tabl where id=decode(?,'escape')", new Object[] {id}, String.class)

似乎 Postgres 会自动将文本转换为 bytea 进行比较,但为什么用 spring jdbc 抛出异常呢?

谢谢

标签: javaspringpostgresqlspring-jdbcbytea

解决方案


推荐阅读