在一个查询中更新多行,但我们期望的输入来自多个数据的 json 对象

2024-01-08

    update users as u set -- postgres FTW
    email = u2.email,
    first_name = u2.first_name,
    last_name = u2.last_name
    from (values
   (1, '[email protected] /cdn-cgi/l/email-protection', 'Hollis', 'O\'Connell'),
   (2, '[email protected] /cdn-cgi/l/email-protection', 'Robert', 'Duncan')
    ) as u2(id, email, first_name, last_name)
    where u2.id = u.id;

上面的查询用于更新一个查询中的多行,它的工作效率也很高,但我有下面的 JSON:

   Person:{[id:1,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"John",last_name:"Doe"],[id:2,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"Robert",last_name:"Duncan"],[id:3,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"Ram",last_name:"Das"],[id:4,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"Albert",last_name:"Pinto"],[id:5,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"Robert",last_name:"Peter"],[id:6,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"Christian",last_name:"Lint"],[id:7,email:"[[email protected] /cdn-cgi/l/email-protection]",first_name:"Mike",last_name:"Hussey"],[id:8,email:"[[email protected] /cdn-cgi/l/email-protection]",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": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "John", "last_name": "Doe"},
        {"id": 2, "email": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "Robert", "last_name": "Duncan"},
        {"id": 3, "email": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "Ram", "last_name": "Das"},
        {"id": 4, "email": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "Albert", "last_name": "Pinto"},
        {"id": 5, "email": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "Robert", "last_name": "Peter"},
        {"id": 6, "email": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "Christian", "last_name": "Lint"},
        {"id": 7, "email": "[[email protected] /cdn-cgi/l/email-protection]", "first_name": "Mike", "last_name": "Hussey"},
        {"id": 8, "email": "[[email protected] /cdn-cgi/l/email-protection]", "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 | [[email protected] /cdn-cgi/l/email-protection] | Doe       | John
  2 | [[email protected] /cdn-cgi/l/email-protection] | Duncan    | Robert
  3 | [[email protected] /cdn-cgi/l/email-protection] | Das       | Ram
  4 | [[email protected] /cdn-cgi/l/email-protection] | Pinto     | Albert
  5 | [[email protected] /cdn-cgi/l/email-protection] | Peter     | Robert
  6 | [[email protected] /cdn-cgi/l/email-protection] | Lint      | Christian
  7 | [[email protected] /cdn-cgi/l/email-protection] | Hussey    | Mike
  8 | [[email protected] /cdn-cgi/l/email-protection] | 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在一个查询中更新多行,但我们期望的输入来自多个数据的 json 对象 的相关文章

  • postgresql 中的锁定表

    我有一个名为 games 其中包含一个名为 title 该列是唯一的 数据库中使用PostgreSQL 我有一个用户输入表单 允许他插入新的 game in games 桌子 插入新游戏的功能会检查之前输入的游戏是否存在 game 与相同的
  • 如何使用 Retrofit 解析嵌套 json....?

    我不知道该怎么办使用 Retrofit 解析 json 熟悉使用 Retrofit 解析简单的 json 但不熟悉解析嵌套Json using Retrofit 这是我的 Json 数据 current observation image
  • 我如何在 C# .NET(win7 手机)中使用“DataContractJsonSerializer”读入“嵌套”Json 文件?

    我有一个问题 如果我的 json 文件看起来像这样 Numbers 45387 Words 空间桶 我可以很好地阅读它 但是如果它看起来像这样 Main Numbers 45387 Words 空间桶 某事 数字 12345 单词 克兰斯基
  • Postgresql 串行错误自动增量

    我在 postgresql 上遇到问题 我认为 postgresql 中有一个错误 我错误地实现了一些东西 有一个表包括colmn1 primary key colmn2 unique colmn3 插入一行后 如果我尝试使用现有的另一次插
  • Angular 4 显示其中的数据

    我不喜欢从 API 返回到我的 Angular 4 应用程序的数据 这是 JSON 的示例 我不关心美元 但这是我正在处理的数据类型 最终目标是在页面上展示 Coin Price BTC 4 281 28 ETH 294 62 etc JS
  • Postgres LIMIT/OFFSET 奇怪的行为

    我正在使用 PostgreSQL 9 6 我有一个这样的查询 SELECT anon 1 id AS anon 1 id anon 1 is valid AS anon 1 is valid anon 1 first name AS ano
  • JSONP 使用 JQuery 从 HTTPS 协议获取 JSON

    我正在尝试获取从 https 安全站点发送的 JSON 客户端希望不要使用任何服务器端语言 全部都是 Javascript 我读到 当使用 Jquery 中的 ajax 函数时 我必须使用 JSONP 才能从安全站点加载 JSON 我的第一
  • Postgres:显示继承的字段

    我应该实现什么查询来获取继承的列 读过this http www alberton info postgresql meta info html综合帖子没有找到解决办法 如果我理解正确的话 您想知道作为表之间继承的一部分的列的名称 SELE
  • 最近邻居的 Postgis SQL

    我正在尝试计算最近的邻居 为此 我需要传递一个参数来限制与邻居的最大距离 例如 半径1000米内最近的邻居是哪些 我做了以下事情 我用数据创建了表 id name latitude longitude 之后 我执行了以下查询 SELECT
  • PostgreSQL 在递归查询中找到所有可能的组合(排列)

    输入是一个长度为 n 的数组 我需要生成数组元素的所有可能组合 包括输入数组中元素较少的所有组合 IN j A B C OUT k A AB AC ABC ACB B BA BC BAC BCA 随着重复 所以AB BA 我尝试过这样的事情
  • IE9 JSON 数据“您要打开还是保存此文件”

    开始使用 IE9 测试我的 jQuery 应用程序 看来我在这里可能遇到麻烦了 我注意到 当我将 JSON 数据返回到 Javascript 方法时 我总是收到此提示 您想打开或保存此文件吗 并为我提供了 3 个按钮 打开 保存和取消 当然
  • 使用 javax/json,如何将元素添加到现有的 JsonArray 中?

    我从文件中读取了一个 JSON 数组 但我想向该数组中添加其他条目 我将如何使用 javax json 库来做到这一点 private String getJson FileInputStream fis throws IOExceptio
  • java -postgresql 最后插入的 id 插入时未获取

    我有一个插入功能postgresql如下 CREATE OR REPLACE FUNCTION insert orderhead order id integer order dt text customer id integer rout
  • 实体类的重建

    我尝试在 netbeans 8 0 1 上运行带有 hibernate spring 和 jpa 的 Web 应用程序 但现在我在编译应用程序时遇到了这个异常 以下是错误 Failed to execute goal org apache
  • Npgsql 参数化查询输出与 PostGIS 不兼容

    我在 Npgsql 命令中有这个参数化查询 UPDATE raw geocoding SET the geom ST Transform ST GeomFromText POINT longitude latitude 4326 3081
  • Google App Engine Flexi 上 Django 的 Postgres 设置

    我正在尝试在应用程序引擎灵活环境中使用 postgres 设置 django 我按照这里的说明进行操作 https cloud google com appengine docs flexible python using cloud sq
  • 如何将 javax.persistence.Column 定义为 Unsigned TINYINT?

    我正在基于 MySQL 数据库中的现有表创建 Java 持久性实体 Bean 使用 NetBeans IDE 8 0 1 我在这个表中遇到了一个字段 其类型为 无符号 TINYINT 3 我发现可以执行以下操作将列的类型定义为 unsign
  • 从 Django 调用 Postgres SQL 存储过程

    我正在开发一个带有 Postgresql 数据库的 Django 项目 我编写了一个可以在 Postgres 上完美运行的存储过程 现在我想从 Django 1 5 调用该存储过程 我已经编写了代码 但它提示错误 CREATE FUNCTI
  • 如何引用下一行的数据?

    我正在 PostgreSQL 9 2 中编写一个函数 对于股票价格和日期的表 我想计算每个条目较前一天的百分比变化 对于最早一天的数据 不会有前一天 因此该条目可以简单地为 Nil 我知道WITH声明可能不应该高于IF陈述 到目前为止 这就
  • Spring 规范 - 谓词的联合

    我需要一个函数来过滤参数并构建查询 我有 4 个参数 因此如果我尝试为每个条件实现查询 我将不得不编写 16 2 4 实施 这不是一个好主意 我尝试通过界面改进我的代码Specification来自 Spring Data JPA 但我无法

随机推荐