Search code examples
spring-bootspring-data-jpabulkinsert

Bulk insert with Spring Boot and Spring Data JPA not working


I know that there are many similar questions about this argument, but I really need a working solution.

I'm trying to configure Spring Boot and Spring Data JPA in order to make bulk insert in a batch.

The target is: commit each N-records, not every single record when making repository.save() action.

What I've tried since now in the application.properties:

spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.generate_statistics=true

But with no success. I've monitored the database and records are persisted in tables one-by-one, not 100-by-100 like I've configured.

UPDATE

Here's the implementation:

@Component
public class BulkInsert {

    @Autowired
    MyRepository repository;

    public void process() {

        PodamFactory podamFactory = new PodamFactoryImpl();

        for(int i=0;i<10000;i++) {
            MyEntity myEntity = podamFactory.manufacturePojo(MyEntity.class);
            repository.save(myEntity);
        }

    }
}

Here's the entity:

@Entity
@Table(name="MYTABLE")
@NamedQuery(name="MyEntity.findAll", query="SELECT m FROM MyEntity m")
public class MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="DESCRIPTION")
    private String description;

    @Id
    @Column(name="ID")
    private String id;

    public MyEntity() {
    }

    // getters and setters

}

And the repository:

public interface MyRepository extends CrudRepository<MyEntity, String> {

}

Solution

  • Try to change your code like this:

    public void process() {
    
        PodamFactory podamFactory = new PodamFactoryImpl();
        List<MyEntity> myEntities = new ArrayList<>(10000);
    
        for(int i = 0; i < 10000; i++) {
            myEntities.add(podamFactory.manufacturePojo(MyEntity.class));
        }
    
        repository.save(myEntities); // for Spring Boot prior 2.0
        // repository.saveAll(myEntities); - for Spring Boot since 2.0
    }
    

    P.S. don't forget to turn on spring.jpa.show-sql to see result

    UPDATE

    Please also check my another answer about bulk insert: How to do bulk (multi row) inserts with JpaRepository?