24 - Hibernate Batch Processing

24.1 Overview

We may want to develop the back end utility or programs where we need to update the bulk data in the database or may need to insert huge number of records. In such scenarios we need to flush the session at a regular interval to make sure that we did not hit OutOfMemoryException because hibernate first level cache is session cache.

Flushing and clearing the session at a regular intervals will persist the data and will reuse the memory and hence we can avoid the out of memory exception. This approach is known as batch processing and in this chapter, we will discuss more about batch processing in hibernate.

24.2 Setting JDBC Batch Size

For Batch processing in hibernate, we need to set the JDBC batch size in hibernate cfg file using hibernate.jdbc.batch_size   property. The ideal value of this property is between 10 to 50.

Make sure you flush and clear the session after the insert and update of records equal to the number you configure in hibernate.jdbc.batch_size property.

For batch updates use Scroll method for optimal performance.

24.3 Example 


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"



          <property name="hibernate.connection.url">
          <property name="hibernate.connection.username">
          <property name="hibernate.connection.password">
          <property name="hibernate.jdbc.batch_size">50</property>   

          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

          <property name="show_sql">true</property>

          <property name="hibernate.connection.driver_class">

          <mapping resource="book-mapping.hbm.xml" />



<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"

   <class name="com.tutorial.hibernate.entity.Book" table="Book">
      <meta attribute="class-description">
         This class contains the employee detail. 
      <id name="id" type="int" column="id">
         <generator class="native"/>
      <property name="isbn" column="isbn" type="string"/>
      <property name="name" column="name" type="string"/> 
      <property name="author" column="author" type="string"/>
      <property name="publisher" column="publisher" type="string"/>
      <property name="price" column="price" type="int"/>

Book Entity

package com.tutorial.hibernate.entity;

public class Book {

       private int id;
    private String isbn;
    private String name;
    private String author;
    private String publisher;
    private int price;
    public int getId() {
        return id;
    public void setId(int id) {
        this.id = id;
    public String getIsbn() {
        return isbn;
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getAuthor() {
        return author;
    public void setAuthor(String author) {
        this.author = author;
    public String getPublisher() {
        return publisher;
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    public int getPrice() {
        return price;
    public void setPrice(int price) {
        this.price = price;
Create Book Table- Use the below create script to create book table.
  'isbn' varchar(255) DEFAULT NULL,
  'name' varchar(255) DEFAULT NULL,
  'author' varchar(255) DEFAULT NULL,
  'publisher' varchar(255) DEFAULT NULL,
  'price' int(11) DEFAULT NULL,
  PRIMARY KEY ('id');

Test Program to insert 1000 records of Book

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.tutorial.hibernate.entity.Book;

public class Test {

       private static SessionFactory factory;
       public static void main(String args[])
          Configuration cfg = new Configuration().configure();
          factory = cfg.buildSessionFactory(); 

          Session session =     factory.openSession();
          Transaction tx = session.beginTransaction();
          for(int i=0;i<1000;i++)
            Book book = new Book();
            book.setAuthor("Author-"+i );
            book.setName("Hibernate Batch-" + i);


Running the above program will insert 1000 records

Like us on Facebook