VOOZH about

URL: https://www.geeksforgeeks.org/java/hibernate-batch-processing/

⇱ Hibernate - Batch Processing - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hibernate - Batch Processing

Last Updated : 25 Mar, 2026

Hibernate stores newly inserted objects in the second-level cache, which can lead to OutOfMemoryException when inserting very large datasets (e.g., millions of records). Batch processing helps efficiently handle such large data operations.

  • Reduces memory usage by processing records in chunks instead of all at once.
  • Improves performance by minimizing database round trips through batching.

Note: Set hibernate.jdbc.batch_size (typically 10–50) to enable batching; a zero or negative value disables it.

Let us use the JPA @TableGenerator annotation to generate the unique key for the entity. As we are not sure of how many records were inserted because of the batch, the IDENTITY generator is disabled by Hibernate.

Step-by-Step Implementation of Hibernate - Batch Processing

Follow the below steps to efficiently implement batch processing in Hibernate for handling large volumes of data.

Step 1: Create Maven Project

  • Create a Maven project (e.g., hibernate-batching-example)
  • Set Java version (1.8 or above)
  • This is a maven-driven project

Project Structure:

👁 Image
 

Step 2: Add Dependencies (pom.xml)

Add required libraries:

  • hibernate-core
  • mysql-connector-java
  • jaxb-api
  • log4j (for logging)

pom.xml

Step 3: Configure Logging

  • Create log4j2.xml
  • Enable Hibernate batch logging for debugging

Step 4: Create Entity Class (Product)

  • Annotate class with @Entity
  • Add fields: id, productName, productBrand, price
  • Use: @Id , @GeneratedValue , @TableGenerator (for batch insert IDs)
  • Add getters, setters, and default constructor

Product.java

Step 5: Create Hibernate Utility Class

  • Create HibernateUtil.java
  • Configure: Database connection (URL, username, password) , Dialect & driver , HBM2DDL_AUTO = update
  • Batch size using:

Environment.STATEMENT_BATCH_SIZE = 50;

  • Build SessionFactory

HibernateUtil.java

Step 6: Implement Batch Insert

Create InsertProductBatchExample.java . Steps are following :

  1. Open session
  2. Begin transaction
  3. Loop (e.g., 100 records)
  4. Save objects using session.save()
  5. After batch size (e.g., 10): Call session.flush() and Call session.clear()
  6. Commit transaction
  7. Close session

InsertProductBatchExample.java

Step 7: Run Insert Program

  • Executes batch insert
  • Data inserted in groups (improves performance)
👁 Image
 

Step 8: Verify Database

  • Check MySQL table
  • Confirm records are inserted in bulk
👁 Image
 

Step 9: Implement Batch Update

Create UpdateProductBatchExample.java Steps:

  1. Open session & transaction
  2. Fetch records using ScrollableResults
  3. Loop through records
  4. Update values (e.g., price)
  5. After batch size: session.flush() and session.clear()
  6. Commit transaction

UpdateProductBatchExample.java

Step 10: Run Update Program

  • Updates records in batches
  • Efficient for large data
👁 Image
 

Step 11: Verify Updated Data

Check updated values in database

MySQL output is as follows:

👁 Image
Comment
Article Tags:
Article Tags: