Showing posts with label batch. Show all posts
Showing posts with label batch. Show all posts

Wednesday, February 18, 2015

Spring Data Custom Batch Save Repository

We use Spring Data JPA for our data access framework and generally we use JpaRepository to create our repos, which is a really handy way of doing it.  Spring uses the SimpleJpaRepository for its implementation and it does have a save that allows an Iterable, but if you look at the code it just loops over the entities and calls the save method on each one.

So, if we want to save, or update, in a batch we need to use something else.  Luckily, Spring JDBC has a pretty handy way of doing it and you can add custom repos to your repository interface.

Like so:

To see the collection util that splits out our collection into batches see Split a Java List Into a List of Sublists

Friday, January 3, 2014

Split a Java List Into a List of Sublists

Often times I'm updating a large amount of records with a batch SQL script, and a lot of times I run into this error: DataIntegrityViolationException: Prepared or callable statement has more than 2000 parameter.

So to help me to update with only the allowed number of parameters I've created a method that will split a List of objects into sublists based on the max number of records you want.  And then I would pass each sublist to the batch update method.