Monday, December 13, 2010

Configuring Multiple Databases in Hibernate

Introduction

Hibernate is designed to be used with a large set of databases. The details of those databases are configured in an XML file called hibernate.cfg.xml. This configuration files could be given any name and is usually placed in the root of your application class path. There are many configuration parameters available that makes the mapping of domain model to relational model easier. The same configurations can be done from your Java class using org.hibernate.cfg.Configuration class.

Sample Application

The sample we discuss here shows how an Employee object can be configured to store in both Oracle Data base and Derby Database. Here we create a POJO class called Employee and store and retrieve its objects from both Oracle and Derby database.
Software Requirements

For this example I have used the following tools.

* NetBeans IDE ( may use Eclipse also)
* Hibernate 3.0
* Oracle 9i , Derby

Sample Project Structure
Employee.java

package hibernatepack.samples;

public class Employee {
private int empid;
private String empname;
private double salary;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}

public String getEmpname() {
return empname;
}

public void setEmpname(String empname) {
this.empname = empname;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}
}

The mapping details of the Employee class are available in the Employee.hbm.xml file:












Since we need to persist the Employee object both in Oracle and in Derby, we need to create 2 configuration files - one for Oracle, another one for Derby.

oracleconfig.cfg.xml






org.hibernate.dialect.OracleDialect
oracle.jdbc.OracleDriver
jdbc:oracle:thin:@10.154.117.76:1521:oracle
user
password
create
true




derbiconfig.cfg.xml






org.hibernate.dialect.DerbyDialect
org.apache.derby.jdbc.ClientDriver
jdbc:derby://localhost:1527/HibernateDB
user
pwd
true
create




The IEmployeeDAO.java lists the operations on the Employee object.


package hibernatepack.samples;
import java.util.List;
public interface IEmployeeDAO {
public void findAllEmployees();
public void insertEmployee(Employee e);
}

Let us Implement the above interface using a class :

EmloyeeDaoImpl.java


public class EmployeeDaoImpl implements IEmployeeDAO {
SessionFactory sessionFactory1 = new Configuration().configure("oracleconfig.cfg.xml").buildSessionFactory();
SessionFactory sessionFactory2 = new Configuration().configure("derbyconfig.cfg.xml").buildSessionFactory();
Session session = null;
Transaction transaction = null;
public void findAllEmployees() {
ArrayList empList = new ArrayList();
try {
session = sessionFactory1.openSession();
transaction = session.beginTransaction();
transaction.begin();
Criteria crit = session.createCriteria(Employee.class);
empList = (ArrayList) crit.list();
System.out.println("Records from Oracle Database");
for (Employee emp : empList) {
System.out.println(emp.getEmpid() + " " + emp.getEmpname() + " " + emp.getSalary());

}
session.close();
session = sessionFactory2.openSession();
Criteria crit1 = session.createCriteria(Employee.class);
empList = (ArrayList) crit1.list();
System.out.println("Records from Derby Database");
for (Employee emp : empList) {
System.out.println(emp.getEmpid() + " " + emp.getEmpname() + " " + emp.getSalary());
}
session.close();
} catch (Exception he) {
he.printStackTrace();
}
}
public void insertEmployee(Employee e) {
try {
session = sessionFactory1.openSession();
transaction = session.beginTransaction();
transaction.begin();
session.save(e);
transaction.commit();
session.close();
session = sessionFactory2.openSession();
transaction = session.beginTransaction();
transaction.begin();
session.save(e);
transaction.commit();
session.close();
} catch (HibernateException he) {
he.printStackTrace();
}
}
}

No comments:

Post a Comment