It’s been ages since I have posted some sample code. It’s mainly because I don’t have time to collect and post sample code anymore. This once was a bit more challenging and googling wasn’t help much, so now that I have some time I though I would post some sample code that achieves batch inserts with spring data.
For example this link:
http://forum.spring.io/forum/spring-projects/data/118203-bulk-insert-with-crudrepository indicated that I had to manually get the session and iterate/flush (which was true when using Spring/Hibernate/JPA). But when using the CRUDRepository it appears it’s much simpler.
Full code sample (maven project) can be found on github: https://github.com/cyrus13/anastasakis-net-sample-code/tree/master/spring-data-batch
You basically need to have the following elements:
- Add: ?rewriteBatchedStatements=true to the end of the connectionstring.
- Make sure you use a generator that supports batching in your entity. E.g.
@GeneratedValue(generator = "generator")
@GenericGenerator(name = "generator", strategy = "increment")
- Use the: save(Iterable<S> paramIterable); method of the JpaRepository to save the data.
- Use the: hibernate.jdbc.batch_size configuration.
So enabling the query log in MySQL:
SET global general_log = 1;
SET global log_output = 'table';
we can see the following mysql code is executed:
select max(id) from ExampleEntity;
insert into ExampleEntity (exampleText, id) values
Note: Don’t forget to stop logging statements into MySQL general log!
SET global general_log = 0;