- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to batch update MySQL table?
- How to do a batch insert in MySQL?
- How to multiple insert or batch insert at a time in MySQL query?
- How to execute UPDATE SQL in a JSP?
- How to execute SQL update query in a JSP?
- How to update listview after insert values in Android sqlite?
- How to validate documents before insert or update in MongoDB?
- Execute INSERT if table is empty in MySQL?
- What is Parameterized Batch Update in JDBC? Explain with an example?
- How to update listview after insert values in Android sqlite using Kotlin?
- Implement INSERT … ON DUPLICATE KEY UPDATE in MySQL
- Implementing INSERT… ON DUPLICATE KEY UPDATE in MySQL
- How to connect hibernate with MySQL Database?
- Use a trigger to stop an insert or update in MySQL?
- Update MySQL table on INSERT command with triggers?
