How to Execute Batch Insert Update in Hibernate?


In this article, we will see how we can execute batch insert/update in hibernate.

Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries to our database table, then we have to make 10 network calls. Instead we can optimize our network calls by using batching. Batching allows us to execute a group of SQL statements in a single network call.

To understand and implement this, let us define our entities −

@Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; // Getters //Setters }

To enable batching in hibernate, we need to add a property to our application properties file:

spring.jpa.properties.hibernate.jdbc.batch_size=3

Now, we need to execute persist function of the EntityManager to insert the data in the database

Example

@Autowired private EntityManager entityManager; @Test Public void InsertInBatch(){ for (int i = 0; i < 6; i++) { Parent parent = Parent[i]; entityManager.persist(parent); } }

Output

"batch":true, "querySize":1, "batchSize":3, "query":["insert into parent (name, id) values (?, ?)"], "params":[["P1","1"],["P2","2"],["P3","3"]]
"batch":true, "querySize":1, "batchSize":3, "query":["insert into parent (name, id) values (?, ?)"], "params":[["P4","4"],["P5","5"],["P6","6"]]

We can see from the console that inserting into parent table is occurring in a batch of size 3.

While persisting the entities, there may occur OutOfMemoryException because Hibernate stores the entities in the persistence context. So, for optimization purpose we can use flush() and clear() of the entity manager after every batch.

Batch Update means updating large amount of data in a single network call.

For batch update, the process is the same. We need to add below two statements in our application properties file and then execute the updating process.

spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.batch_versioned_data=true

Example

Code to update the data −

@Autowired private EntityManager entityManager; @Test public void UpdateInBatch() { TypedQuery<Parent> query = entityManager.createQuery("SELECT p from Parent p", Parent.class); List<Parent> Parents = query.getResultList(); int i=1; for (Parent parent : Parents) { String s="Parent"+Integer.toString(i); i++; parent.setName(s); } }

Hibernate will now bind these statements together in a single batch and executes them.

Output

"batch":true, "querySize":1, "batchSize":3, "query":["update parent set name=? where id=?"], "params":[["Parent1","1"],[" Parent2","2"],[" Parent3","3"]]
"batch":true, "querySize":1, "batchSize":3, "query":["update parent set name=? where id=?"], "params":[["Parent4","4"],["Parent5","5"],["Parent6","6"]]

We can see from the console that updating data in parent table is occurring in a batch of size 3.

Updated on: 26-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements