Home > Java > javaTutorial > How to perform bulk insert update operations in Hibernate?

How to perform bulk insert update operations in Hibernate?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-27 23:17:06
forward
1464 people have browsed it

How to perform bulk insert update operations in Hibernate?

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
}
Copy after login

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
Copy after login

Now, we need to execute EntityManager’s persist functionInsert data into 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);
   }
}
Copy after login

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"]]
Copy after login

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
Copy after login

Example

Code to update 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);
   }
}
Copy after login

Hibernate will now bundle these statements in a batch and execute 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"]]
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template