PostgreSQL JDBC Null字符串作为字节 [英] PostgreSQL JDBC Null String taken as a bytea

查看:481
本文介绍了PostgreSQL JDBC Null字符串作为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果entity.getHistory()为空,则遵循以下代码片段:

If entity.getHistory() is null following code snippet:

(getEntityManager()返回弹簧注入的EntityManager,数据库字段的历史记录类型为:文本或varchar2(2000)

(getEntityManager() returns spring injected EntityManager, database field history type is: text or varchar2(2000)

Query query = getEntityManager().createNativeQuery("insert into table_name(..., history, ....) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
[...]
.setParameter(6, entity.getHistory())
[...]

query.executeUpdate();

给出了奇怪的例外:

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**

提示:您将需要重写或强制转换表达式.

Hint: You will need to rewrite or cast the expression.

仅在此配置中出现问题:
操作系统:CentOS版本5.6(最终版)
Java:1.6.0_26
DB:PostgreSQL 8.1
JDBC驱动程序:postgresql-9.1-901.jdbc4
应用程序服务器:apache-tomcat-6.0.28

Problem occurred only in this configuration:
OS: CentOS release 5.6 (Final)
Java: 1.6.0_26
DB: PostgreSQL 8.1
JDBC Driver: postgresql-9.1-901.jdbc4
Application Server: apache-tomcat-6.0.28

在其他一些配置上或当历史记录为空字符串时,一切都可以正常工作. 从pgAdmin执行的同一条语句可以正常工作.

Everything is working fine on few other configurations or when history is empty string. Same statement executed from pgAdmin works fine.

我猜问题出在PostgreSQL JDBC驱动程序中,是否有一些明智的理由将空字符串当作BYtea值?也许Postgres 8和9之间有一些奇怪的变化?

I guess problem is in PostgreSQL JDBC driver, is there some sensible reason to treat null string as a bytea value? Maybe some strange changes between Postgres 8 and 9?

推荐答案

有一个简单的解决方法:构建查询字符串时,如果参数将为null,请使用"null"而不是?".

There's a simple fix: when you're building the query string, if a parameter is going to be null -- use "null" instead of "?".

就像@araqnid所说的那样,Hibernate错误地将空值转换为bytea类型,因为它没有更好的了解.

As @araqnid says, Hibernate is incorrectly casting null values into type bytea because it doesn't know any better.

这篇关于PostgreSQL JDBC Null字符串作为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆