1

I'm using spring-data-jpa 1.x in my project and there is a bulk-insert scenario like:

List<Entity> entities = ...;
entities.forEach(repository::save);

But I have millions of entities to insert and it takes too long time due to database connection latency. I need something like:

repository.saveAll(entities);

to speed up my insertion.

Up to spring-data-jpa:1.11.23.RELEASE which is the latest version of spring-data-jpa 1.x, there is still no such method like saveAll().

=== UPDATE ===

In spring-data-jpa 1.x the method name is save():

<S extends T> List<S> save(Iterable<S> entities)

=== UPDATE-2 ===

Just using save() (spring-data-jpa 1.x) or saveAll() (spring-data-jpa 2.x) can't make hibernate generate multi-insert SQL like INSERT INTO table VALUES (?, ?, ...), (?, ?, ...), .... This is another topic, see: M. Deinum and Marc Ströbel's comment, or How to do bulk (multi row) inserts with JpaRepository?

auntyellow
  • 2,423
  • 2
  • 20
  • 47
  • Why would `saveAll` won't work? https://www.baeldung.com/spring-data-jpa-batch-inserts – Sid Aug 17 '20 at 08:19
  • 1
    There is `save` which takes a list (which internally calls save per element). You need to configure your JPA provider (assuming hibernate) to batch inserts/updates AND when using MySQL configure that to rewrite the query!.Also don't insert everything at once insert x elements then flush and clear your entitymanager. X you will need to determine for yourself. – M. Deinum Aug 17 '20 at 08:33
  • 2
    there's a spring property for hibernate: `spring.jpa.properties.hibernate.jdbc.batch_size=5` https://www.baeldung.com/jpa-hibernate-batch-insert-update – Marc Stroebel Aug 17 '20 at 10:51

0 Answers0