The largest Interview Solution Library on the web


Hibernate Tutorials/
« Previous | 1 | 2 | 3 | Next »
Let us modify configuration file as to add hibernate.jdbc.batch_sizeproperty:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<propertyname="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<propertyname="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>

<!-- Assume students is the database name -->
<propertyname="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<propertyname="hibernate.connection.username">
root
</property>
<propertyname="hibernate.connection.password">
root123
</property>
<propertyname="hibernate.jdbc.batch_size">
50
</property>

<!-- List of XML mapping files -->
<mappingresource="Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>

Consider the following POJO Employee class:

publicclassEmployee{
privateint id;

privateString firstName;
privateString lastName;
privateint salary;

publicEmployee(){}
publicEmployee(String fname,String lname,int salary){
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
publicint getId(){
return id;
}
publicvoid setId(int id ){
this.id = id;
}
publicString getFirstName(){
return firstName;
}
publicvoid setFirstName(String first_name ){
this.firstName = first_name;
}
publicString getLastName(){
return lastName; }

publicvoid setLastName(String last_name ){
this.lastName = last_name;
}
publicint getSalary(){
return salary;
}
publicvoid setSalary(int salary ){
this.salary = salary;
}
}

Let us create the following EMPLOYEE table to store Employee objects:

create table EMPLOYEE (
id INT NOT NULL auto_increment,
first_name VARCHAR(20)default NULL,
last_name VARCHAR(20)default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);

Following will be mapping file to map Employee objects with EMPLOYEE table.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<classname="Employee"table="EMPLOYEE">
<metaattribute="class-description">
This class contains the employee detail.
</meta>
<idname="id"type="int"column="id">
<generatorclass="native"/>
</id>
<propertyname="firstName"column="first_name"type="string"/>
<propertyname="lastName"column="last_name"type="string"/>
<propertyname="salary"column="salary"type="int"/>
</class>
</hibernate-mapping>

Finally, we will create our application class with the main() method to run the application where we will use flush() and clear() methods available with Session object so that Hibernate keep writing these records into the database instead of caching them in the memory.

import java.util.*;

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

publicclassManageEmployee{
privatestaticSessionFactory factory;
publicstaticvoid main(String[] args){
try{
factory=newConfiguration().configure().buildSessionFactory();
}catch(Throwable ex){
System.err.println("Failed to create sessionFactory object."+ex);
thrownewExceptionInInitializerError(ex);
}
ManageEmployee ME =newManageEmployee();

/* Add employee records in batches */
ME.addEmployees();
}
/* Method to create employee records in batches */
publicvoid addEmployees(){
Session session =factory.openSession();
Transactiontx=null;
Integer employeeID =null;
try{
tx= session.beginTransaction();
for(int i=0; i<100000; i++){
String fname ="First Name "+ i;
String lname ="Last Name "+ i;
Integer salary = i;
Employee employee =newEmployee(fname, lname, salary);
session.save(employee);
if( i %50==0){
session.flush();
session.clear();
}
}
tx.commit();
}catch(HibernateException e){
if(tx!=null) tx.rollback();
e.printStackTrace();
}finally{
session.close();
}
return;
}
}
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com