有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringJDBC在比较PostgresBytea和string时引发异常

我有一张这样的桌子

create table tabl(id bytea, val text);

&;像这样的问题

select * from tabl where id='foo';

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

但是当SpringJDBCTemplate执行相同的查询时,它会抛出一个异常

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进行比较,但是为什么要在SpringJDBC中抛出异常呢

谢谢


共 (0) 个答案