Batch Processing Example in Hibernate

Hi Friends ,let’s go through an example to understand batch processing in hibernate.Before moving to the example itself ,first of all, I would like to mention what is batch processing with respect to a database and what is real developer challenges while performing batch processing using hibernate.

What is batch processing?

Batch processing with respect to a database is performing a collection  of database transactions.

Challenges associated with batch processing in hibernate.

API required for batch processing is not different from general Hibernate related API.To understand the challenge lets assume there is a requirement of performing  insert or  update on a large number of record like 1000000.In such scenario the batch processing would not work normally ,it throws OutOfMemoryException at a certain point.
Now, the question is why this Exception occurs? To explain this let’s dig it little more.Actually when we perform  or session.update(obj) or session.saveUpdate(obj), the obj value not saved to database until the commit() get executed.It persists in the hibernate session level cache . The hibernate session level cache stored in JVM.Hence after persisting the allowed size of data in JVM it throws  OutOfMemoryException.
To avoid this problem we flush the cache after executing any of  or session.update(obj) or session.saveUpdate(obj)  for 20-50 times.So Everytime we execute session.flush() ,it will release the memory.

Sample Code

Here is the sample code for batch processing in Hibernate-In this example, I am taking a real life scenario, like online Order for a product on product launch day.This is just an example taken for explaining the batch processing.

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<1000000; i++ )
Order order= new Order("o"+i,String.valueOf(i),new Date());;
if( i % 50 == 0 )
session.flush();//flush after a batch insertion and release memory:

ORM /Entity Class

package com.nik.hibernate.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Table(name = "ORDER")
public class Order implements Serializable {

 private static final long serialVersionUID = -2955407588841152439L;

 @Column(name = "order_id")
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private String orderID;
 @Column(name = "customer_id")
 private String customerId;
 @Column(name = "order_date")
 private Date orderDate;

 Order(String orderId, String customerID, Date orderDate) {
 this.orderID = orderId;
 this.customerId = customerID;
 this.orderDate = orderDate;

 public String getOrderID() {
 return orderID;

 public void setOrderID(String orderID) {
 this.orderID = orderID;

 public String getCustomerId() {
 return customerId;

 public void setCustomerId(String customerId) {
 this.customerId = customerId;

 public Date getOrderDate() {
 return orderDate;

 public void setOrderDate(Date orderDate) {
 this.orderDate = orderDate;


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">toor</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="">update</property>
 <property name="hibernate.jdbc.batch_size"> 50 </property>

Hope this will help you to understand ,how we can implement batch processing using hibernate.

We will come up with more hibernate related post, till then stay tuned and Happy Coding!


Leave a Reply

Your email address will not be published. Required fields are marked *