【发布时间】:2012-01-02 21:58:22
【问题描述】:
如果 entity.getHistory() 在代码 sn-p 之后为空:
(getEntityManager()返回spring注入的EntityManager,数据库字段历史类型为:text或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**
提示:您需要重写或转换表达式。
仅在此配置中出现问题:
操作系统:CentOS 5.6 版(最终版)
Java:1.6.0_26
数据库:PostgreSQL 8.1
JDBC 驱动程序:postgresql-9.1-901.jdbc4
应用服务器:apache-tomcat-6.0.28
在少数其他配置或历史为空字符串时一切正常。 从 pgAdmin 执行的相同语句工作正常。
我猜问题出在 PostgreSQL JDBC 驱动程序中,是否有合理的理由将空字符串视为字节值?也许 Postgres 8 和 9 之间有一些奇怪的变化?
【问题讨论】:
-
我怀疑 EntityManager 为 NULL 值调用了错误的
PreparedStatement.setXXX()方法。您也可以尝试 8.1 JDBC 驱动程序。顺便说一句:您是否知道不再支持 8.1? -
你能澄清一下你的前两段吗?它们似乎没有意义。
-
相同的代码在 PostgreSQL 9+(@a_horse_with_no_name 客户端必须有 8.1...)、Windows 7 等环境下运行良好。如果 entity.getHistory() 为 "" 而不是 null,则它可以正常工作。从 pgAdmin 执行的相同插入工作正常。
-
@laidlook:您是否针对 8.1 数据库尝试了 8.1 驱动程序? (但我仍然认为 EntityManager 的事情有问题)
-
有点。我无权访问该服务器,我所能做的就是要求客户做这种事情。他们没有结果,所以他们说...悲伤的开发人员生活 :) 我将尝试在 VM 上创建此配置。
标签: string postgresql hibernate jdbc null