[英]The column index is out of range: 2, number of columns: 1 error while updating jsonb column
我正在嘗試使用 mybatis 更新 java 中的 jsonb 列。
以下是我的映射器方法
@Update("update service_user_assn set external_group = external_group || '{\"service_name\": \"#{service_name}\" }' where user=#{user} " +
" and service_name= (select service_name from services where service_name='Google') " )
public int update(@Param("service_name")String service_name,@Param("user") Integer user);
更新 jsonb (external_group) cloumn 時出現以下錯誤。
### Error updating database. Cause: org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.
### The error may involve com.apds.mybatis.mapper.ServiceUserMapper.update-Inline
我能夠以相同的方式更新非 jsonb 列。
此外,如果我放置硬編碼值,它適用於 jsonb 列。
更新 jsonb 列時如何解決此錯誤?
您不應將#{}
括在單引號中,因為它將成為文字的一部分而不是占位符。 IE
external_group = external_group || '{"service_name": "?"}' where ...
因此, PreparedStatement
中將只有一個占位符,並且您會收到錯誤消息。
正確的方法是連接SQL中的#{}
。
您可能還需要將文字顯式轉換為jsonb
類型。
@Update({
"update service_user_assn set",
"external_group = external_group",
"|| ('{\"service_name\": \"' || #{service_name} || '\" }')::jsonb",
"where user=#{user} and",
"service_name= (select service_name from services where service_name='Google')"})
正在執行的 SQL 將如下所示。
external_group = external_group || ('{"service_name": "' || ? || '"}')::jsonb where ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.