gpt4 book ai didi

string - PostgreSQL JDBC Null 字符串作为一个 bytea

转载 作者:行者123 更新时间:2023-11-29 11:14:05 24 4
gpt4 key购买 nike

如果 entity.getHistory() 为 null 以下代码片段:

(getEntityManager() 返回 spring 注入(inject)的 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 release 5.6 (Final)
Java: 1.6.0_26
数据库:PostgreSQL 8.1
JDBC驱动:postgresql-9.1-901.jdbc4
应用服务器:apache-tomcat-6.0.28

在少数其他配置上或历史为空字符串时一切正常。从 pgAdmin 执行的相同语句工作正常。

我想问题出在 PostgreSQL JDBC 驱动程序中,是否有合理的理由将空字符串视为 bytea 值?也许 Postgres 8 和 9 之间有一些奇怪的变化?

最佳答案

有一个简单的解决方法:当您构建查询字符串时,如果参数将为 null——使用“null”而不是“?”。

正如@araqnid 所说,Hibernate 错误地将 null 值转换为 bytea 类型,因为它不知道更好。

关于string - PostgreSQL JDBC Null 字符串作为一个 bytea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8211195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com