我正在尝试使用 Spring JPA 实现以下目标。
update test as t set
column_a = c.column_a
from (values
('123', 1),
('345', 2) // this will be dynamic
) as c(column_b, column_a)
where c.column_b = t.column_b;
有没有办法使用本机查询从 spring jpa 传递值?
我建议您首先将动态数据序列化为任意长度的 JSON 数组 - 甚至是空数组 - 像这样
[
[1, "234"],
[2, "345"]
]
并使用这个本机查询:
@Query(
value = "update test as t set column_a = c.column_a
from (
select j[0]\\:\\:integer column_a, j[1]\\:\\:text column_b
from jsonb_array_elements (?1\\:\\:jsonb) j
) c
where c.column_b = t.column_b",
nativeQuery = true
)
备注:
::
被转义为 \\:\\:
(j->>0)\\:\\:integer
和 j->>1
;