【发布时间】:2017-10-31 13:02:55
【问题描述】:
我正在使用带有 JDBC 模板的 spring 框架,并且我也在使用 postgres。
我在 postgres 中有使用 UUID 作为主键的表,该列的类型是 postgres 的native UUIDs。如何将这些 UUID 存储在通过 JDBC 模板创建的准备好的语句中?
我尝试将 UUID 转换为这样的字符串:
int rowsAffected = this.jdbc.update(sql, new Object[] {
baseMaterial.getId().toString().toLowerCase(),
baseMaterial.getName(),
baseMaterial.getDescription()
});
但这会导致这个错误:
ERROR: column "id" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
如果我像这样使用原始 UUID:
int rowsAffected = this.jdbc.update(sql, new Object[] {
baseMaterial.getId(),
baseMaterial.getName(),
baseMaterial.getDescription()
});
然后我会遇到这个错误:
org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.UUID. Use setObject() with an explicit Types value to specify the type to use.
有什么想法吗?这让我发疯了。
【问题讨论】:
-
您使用的是哪个版本的 PostgreSQL JDBC 驱动程序?
-
@MarkRotteveel
<version>9.1-901-1.jdbc4</version> -
我建议你更新到 42.1.4 看看是否能解决你的问题,9.1-901 已经 6 岁了。
-
@MarkRotteveel 果然,解决了它。将其发布为答案,我会接受它
标签: java spring postgresql jdbc jdbctemplate