postgresql - Postgres 类型转换和参数绑定冲突
问题描述
我收到此错误:
org.postgresql.util.PSQLException: No value specified for parameter 1.
对于这种查询:
UPDATE user
SET Email = 'fake.' || id::varchar || '@localhost'
WHERE Email is not null and char_length(Email)>0 and removed=false and id in (:ids)
我将此查询作为本机查询运行,如下所示:
sessionFactory.getCurrentSession()
.createSQLQuery(sql)
.setParameterList("ids", ids)
.executeUpdate()
;
似乎id::varchar
混淆了 Hibernate 或 JDBC。有什么办法可以逃避这种情况吗?用于类型转换的非常标准的 Postgres 语法。不喜欢强制转换语法,更喜欢使用这种类型映射。
解决方案
使用concat()
which 将自动转换 ID:
UPDATE user
SET Email = concat('fake.', id, '@localhost')
WHERE Email is not null
and char_length(Email) > 0
and removed = false
and id in (:ids)
或者使用标准 CAST 运算符:
UPDATE user
SET Email = 'fake.' || cast(id as text) || '@localhost'
WHERE Email is not null
and char_length(Email) > 0
and removed = false
and id in (:ids)
推荐阅读
- java - 定义构造函数类并初始化值
- excel - 如何将枚举作为文本传递到 VBA?
- java - 在 AWS lambda 中运行 EJB 应用程序
- tensorflow-lite - “如何将单个图像传递给 Tensorflow lite”
- r - 3D 分类图没有成比例的线条长度
- python - 如何在 matplotlib 上转换标签值
- javascript - 如何将数组中的每个偶数值都涂成蓝色
- 带有 Javascript 的元素?
- android - Kotlin 获取一个 EditText 的值
- python - 如何在 Python 3 中交换两对字节
- spring-boot - 使用 Spring Kafka-Syncronous 反序列化来自同一 Kafka 主题的不同 JSON 有效负载