In this article, we will see how to perform bulk insert/update in Hibernate.
Every time we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries into the database table, then we have to make 10 network calls. Instead, we can optimize network calls by using batch processing. Batch processing allows us to execute a set of SQL statements in a single network call.
To understand and implement this, let’s define our entity −
@Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String name; // Getters //Setters }
In order 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 executeEntityManager’s persist functionInsert data into the database
@Autowired private EntityManager entityManager; @Test Public void InsertInBatch(){ for (int i = 0; i < 6; i++) { Parent parent = Parent[i]; entityManager.persist(parent); } }
"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 the insertion into the parent table is performed with a batch size of 3.
When persisting entities, OutOfMemoryException may occur because Hibernate stores entities in the persistence context. Therefore, for optimization purposes, we can use the entity manager's flush() and clear() after each batch.
Batch update means updating a large amount of data in one network call.
For batch updates, the process is the same. We need to add the following two statements in the application properties file and then perform the update process.
spring.jpa.properties.hibernate.order_updates=true spring.jpa.properties.hibernate.batch_versioned_data=true
Code to update data−
@Autowired private EntityManager entityManager; @Test public void UpdateInBatch() { TypedQueryquery = entityManager.createQuery("SELECT p from Parent p", Parent.class); List Parents = query.getResultList(); int i=1; for (Parent parent : Parents) { String s="Parent"+Integer.toString(i); i++; parent.setName(s); } }
Hibernate will now bundle these statements in a batch and execute them.
"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"]]
As you can see from the console, the data update in the parent table is performed with a batch size of 3.
The above is the detailed content of How to perform bulk insert update operations in Hibernate?. For more information, please follow other related articles on the PHP Chinese website!