This post explain batch processing in hibernate.Before moving to the example itself ,it would be better to understand what is batch processing and why this concept comes into the scenario.
@Table(name="ORDER")
public class Order implements Serializable {
private static final long serialVersionUID = -2955407588841152439L;
@Id
@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() {
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.jdbc.batch_size"> 50 </property>
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------------------
Batch processing in hibernate or related to database is performing collection of database transactions.
API required for batch processing is nothing different from general Hibernate related api, but when there is a requirement of performing insert,update on a large number of record like 1000000.In such scenario the batch processing would not work normally ,it throws OutOfMemoryException.
Actually when we perform session.save(obj) or session.update(obj) or session.saveUpdate(obj) the value not saved to database until the commit() get executed,it persist 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 performing any of session.save(obj) or session.update(obj) or session.saveUpdate(obj) .
The below given example is of 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.
Code Block
-----------------------------------------------------------------------------------------
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());
session.save(order);
if( i % 50 == 0 )
{
session.flush();//flush after a batch insertion and release memory:
session.clear();
}
}
tx.commit();
session.close();
----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
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());
session.save(order);
if( i % 50 == 0 )
{
session.flush();//flush after a batch insertion and release memory:
session.clear();
}
}
tx.commit();
session.close();
----------------------------------------------------------------------------------------
ORM /Entity Calss
package com.nik.hibernate.pojo;
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;
@Entityimport java.io.Serializable;
@Table(name="ORDER")
public class Order implements Serializable {
private static final long serialVersionUID = -2955407588841152439L;
@Id
@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(DateorderDate) {this.orderDate = orderDate;}}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.jdbc.batch_size"> 50 </property>
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------------------