update users as u set -- postgres FTW
email = u2.email,
first_name = u2.first_name,
last_name = u2.last_name
from (values
(1, 'hollis@weimann.biz', 'Hollis', 'O\'Connell'),
(2, 'robert@duncan.info', 'Robert', 'Duncan')
) as u2(id, email, first_name, last_name)
where u2.id = u.id;
上面的查询用于在一个查询中更新多行,它也能有效地工作,但我在下面有一个 JSON:
Person:{[id:1,email:"[xyz@abc.com]",first_name:"John",last_name:"Doe"],[id:2,email:"[xyz@abc.com]",first_name:"Robert",last_name:"Duncan"],[id:3,email:"[xyz@abc.com]",first_name:"Ram",last_name:"Das"],[id:4,email:"[xyz@abc.com]",first_name:"Albert",last_name:"Pinto"],[id:5,email:"[xyz@abc.com]",first_name:"Robert",last_name:"Peter"],[id:6,email:"[xyz@abc.com]",first_name:"Christian",last_name:"Lint"],[id:7,email:"[xyz@abc.com]",first_name:"Mike",last_name:"Hussey"],[id:8,email:"[xyz@abc.com]",first_name:"Ralph",last_name:"Hunter"]};
这样的 JSON 有 1000 个数据,我想使用 JPA 将其插入到数据库中。目前我已经通过迭代它来插入它,这使得我的代码变慢了,是否有任何其他可以实现的替代方案。
我们将不胜感激。
这是我的 Java 代码:
public Boolean multiEditPerson(List<PersonList> personList) {
for (PersonList list : personList) {
Person personMstr = em.find(Person.class, list.getId());
personMstr.setFirstName(list.getFirstName());
personMstr.setLastName(list.getLastName());
personMstr.setEmail(Arrays.toString(list.getEmail()));
em.persist(personMstr);
}
return Boolean.TRUE;
}
最佳答案
您可以根据 json 文档进行批量插入。您应该重新格式化文档,因为问题中显示的格式很奇怪且不切实际。
完整的工作示例:
create table example(id int primary key, email text, last_name text, first_name text);
with jsondata(jdata) as (
values
(
'[
{"id": 1, "email": "[xyz@abc.com]", "first_name": "John", "last_name": "Doe"},
{"id": 2, "email": "[xyz@abc.com]", "first_name": "Robert", "last_name": "Duncan"},
{"id": 3, "email": "[xyz@abc.com]", "first_name": "Ram", "last_name": "Das"},
{"id": 4, "email": "[xyz@abc.com]", "first_name": "Albert", "last_name": "Pinto"},
{"id": 5, "email": "[xyz@abc.com]", "first_name": "Robert", "last_name": "Peter"},
{"id": 6, "email": "[xyz@abc.com]", "first_name": "Christian", "last_name": "Lint"},
{"id": 7, "email": "[xyz@abc.com]", "first_name": "Mike", "last_name": "Hussey"},
{"id": 8, "email": "[xyz@abc.com]", "first_name": "Ralph", "last_name": "Hunter"}
]'::jsonb)
)
insert into example
select (elem->>'id')::int, elem->>'email', elem->>'last_name', elem->>'first_name'
from jsondata,
jsonb_array_elements(jdata) as elem;
结果:
select *
from example
id | email | last_name | first_name
----+---------------+-----------+------------
1 | [xyz@abc.com] | Doe | John
2 | [xyz@abc.com] | Duncan | Robert
3 | [xyz@abc.com] | Das | Ram
4 | [xyz@abc.com] | Pinto | Albert
5 | [xyz@abc.com] | Peter | Robert
6 | [xyz@abc.com] | Lint | Christian
7 | [xyz@abc.com] | Hussey | Mike
8 | [xyz@abc.com] | Hunter | Ralph
(8 rows)
如果要更新表(而不是插入表):
with jsondata(jdata) as (
-- values as above
)
update example set
email = elem->>'email',
last_name = elem->>'last_name',
first_name = elem->>'first_name'
from jsondata,
jsonb_array_elements(jdata) as elem
where id = (elem->>'id')::int;
关于json - 在一个查询中更新多行,但我们期望的输入来自多个数据的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728208/