简体   繁体   English

如何批量/批量保存或更新大量实体?

[英]How to save or update a lot of entities in a bulk/batch?

I have an entity that follows this structure:我有一个遵循这种结构的实体:

name: String(not null, unique),
value: Integer(not null)
id: Long(generated, not null, unique)

I have a list of entities(List<>), and I want to save them to a table or update if some were already in a table, but I need an effective way to do that with some bulk/batch insert/update.我有一个实体列表 (List<>),我想将它们保存到一个表中,或者如果表中已经有一些实体,我想更新它们,但我需要一种有效的方法来通过一些批量/批量插入/更新来做到这一点。 Now I've tried repository.saveAll and entityManager.merge, but I get the repeated value error, as it doesn't update the values that're already in a table.现在我已经尝试了 repository.saveAll 和 entityManager.merge,但是我得到了重复值错误,因为它没有更新表中已有的值。 What should I do?我应该怎么办?

Please refer to this topic:请参考本主题:

How to do bulk (multi row) inserts with JpaRepository? 如何使用 JpaRepository 进行批量(多行)插入?

To get a bulk insert with Sring Boot and Spring Data JPA you need only two things:要使用 Sring Boot 和 Spring Data JPA 进行批量插入,您只需要两件事:

  1. set the option spring.jpa.properties.hibernate.jdbc.batch_size to appropriate value you need (for example: 20).将选项spring.jpa.properties.hibernate.jdbc.batch_size设置为您需要的适当值(例如:20)。
  2. use saveAll() method of your repo with the list of entities prepared for inserting.使用 repo 的saveAll()方法和准备插入的实体列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM