'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. 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. What should I do?
Solution 1:[1]
Please refer to this topic:
How to do bulk (multi row) inserts with JpaRepository?
To get a bulk insert with Sring Boot and Spring Data JPA you need only two things:
- set the option
spring.jpa.properties.hibernate.jdbc.batch_sizeto appropriate value you need (for example: 20). - use
saveAll()method of your repo with the list of entities prepared for inserting.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohamed Aymen Charrada |
