Table of Contents
Example
Output
Home Java javaTutorial How to perform bulk insert update operations in Hibernate?

How to perform bulk insert update operations in Hibernate?

Aug 27, 2023 pm 11:17 PM
hibernate Batch update Batch insert

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps and precautions for implementing batch updates using Oracle stored procedures Steps and precautions for implementing batch updates using Oracle stored procedures Mar 08, 2024 pm 04:12 PM

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

How to integrate Hibernate in SpringBoot project How to integrate Hibernate in SpringBoot project May 18, 2023 am 09:49 AM

Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

Java Errors: Hibernate Errors, How to Handle and Avoid Java Errors: Hibernate Errors, How to Handle and Avoid Jun 25, 2023 am 09:09 AM

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

Summary of frequently asked questions about importing Excel data into Mysql: How to solve the problem of large batch insertion when importing data? Summary of frequently asked questions about importing Excel data into Mysql: How to solve the problem of large batch insertion when importing data? Sep 08, 2023 am 11:07 AM

Summary of frequently asked questions about importing Excel data into Mysql: How to solve the problem of large batch insertion when importing data? Importing Excel data into MySQL is one of the tasks often encountered in daily development. For importing a small amount of data, you can use database client tools or command lines to perform insertion operations. But when faced with large batches of data import, a simple single insert operation will undoubtedly cause serious performance problems. This article will describe how to solve this problem and give corresponding code examples. Problem description: In actual use,

What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate May 27, 2023 pm 05:06 PM

Hibernate's one-to-many and many-to-many Hibernate is an excellent ORM framework that simplifies data access between Java applications and relational databases. In Hibernate, we can use one-to-many and many-to-many relationships to handle complex data models. Hibernate's one-to-many In Hibernate, a one-to-many relationship means that one entity class corresponds to multiple other entity classes. For example, an order can correspond to multiple order items (OrderItem), and a user (User) can correspond to multiple orders (Order). To implement a one-to-many relationship in Hibernate, you need to define a collection attribute in the entity class to store

What are the differences between hibernate and mybatis What are the differences between hibernate and mybatis Jan 03, 2024 pm 03:35 PM

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

Application cases of Oracle stored procedure batch update in data processing Application cases of Oracle stored procedure batch update in data processing Mar 08, 2024 am 10:24 AM

Application cases of Oracle stored procedure batch update in data processing In actual data processing, we often need to update a large amount of data in the database. Oracle database provides the function of stored procedures, which can effectively handle these large batch data update operations and improve data processing efficiency and performance. In this article, we will introduce the application case of batch update of Oracle stored procedures and provide specific code examples to help readers better understand and use this function. Case Background Suppose we have a

The tacit cooperation between Vue and Excel: how to achieve batch update and import of data The tacit cooperation between Vue and Excel: how to achieve batch update and import of data Jul 22, 2023 pm 09:03 PM

The tacit cooperation between Vue and Excel: How to realize batch update and import of data Introduction: With the rapid development of information technology, Excel tables, as a popular data management tool, are widely used in various industries and fields. At the same time, Vue is also widely popular as a flexible and efficient front-end development framework. This article will introduce how to achieve batch update and import of data through the tacit cooperation of Vue and Excel. To help readers understand better, we will give code examples. Implement data batch update: in V

See all articles