Multiple databases using hibernate example
Database and Table Structure:
[plain gutter=”false”]
DATABASE : DATABASEONE
Table: EMPIDNAME
EMPID INT(4)
EMPNAME VARCHAR(15)
DATABASE : DATABASETWO
Table : EMPIDSALARY
EMPID INT (4)
EMPSALARY INT(4)
[/plain]
Project Structure:
Step 1: Hibernate configuration files for both databases:
first database (DATABASEONE) hibernate configuration file (hibernate1.cfg.xml):
[xml highlight=”8″]
<?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>
<!– Database connection configurations –>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/DatabaseOne</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!– Making autocommit false –>
<property name="hibernate.connection.autocommit">false</property>
<!– Displaying sql queries when running the program –>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!– For JDBC connections –>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!– Mapping file –>
<mapping resource="EmployeeIdName.hbm.xml" />
</session-factory>
</hibernate-configuration>
[/xml]
second database (DATABASETWO) hibernate configuration file(hibernate2.cfg.xml):
[xml highlight=”8″]
<?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>
<!– Database connection configurations –>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/DatabaseTwo</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!– Making autocommit false –>
<property name="hibernate.connection.autocommit">false</property>
<!– Displaying sql queries when running the program –>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!– For JDBC connections –>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!– Mapping file –>
<mapping resource="EmployeeIdSalary.hbm.xml" />
</session-factory>
</hibernate-configuration>
[/xml]
Step 2: Class files for the above created tables to map in the hibernate mapping file below (step3),
EmpIdName.java:
[java]
public class EmpIdName {
private int empid;
private String empname;
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;
}
}
[/java]
EmpIdSalary.java:
[java]
public class EmpIdSalary {
private int empid;
private int empsalary;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public int getEmpsalary() {
return empsalary;
}
public void setEmpsalary(int empsalary) {
this.empsalary = empsalary;
}
}
[/java]
Step 3: Hibernate mapping files to map the above created classes with tables and its columns,
EmployeeIdName.hbm.xml (to map EmpIdName.java):
[xml highlight=”6″]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="EmpIdName" table="EMPIDNAME">
<id name="empid" column="EMPID" type="integer">
<generator class="assigned"></generator>
</id>
<property name="empname" column="EMPNAME" type="string"></property>
</class>
</hibernate-mapping>
[/xml]
EmployeeIdSalary.hbm.xml (to map EmpIdSalary.java):
[xml highlight=”5″]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="EmpIdSalary" table="EMPIDSALARY">
<id name="empid" column="EMPID" type="integer">
<generator class="assigned"></generator>
</id>
<property name="empsalary" column="EMPSALARY" type="integer"></property>
</class>
</hibernate-mapping>
[/xml]
Step 4: Main class to retrive the data’s,
EmpIdNameSalary.java:
[java highlight=”13-16,32,47″]
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class EmpIdNameSalary {
public static void main(String[] args) {
SessionFactory hibernateConfig1 = new Configuration().configure(
"hibernate1.cfg.xml").buildSessionFactory();
SessionFactory hibernateConfig2 = new Configuration().configure(
"hibernate2.cfg.xml").buildSessionFactory();
Session session = null;
Transaction transaction = null;
List empIdNameList = new ArrayList();
List empIdSalaryList = new ArrayList();
try {
session = hibernateConfig1.openSession();
transaction = session.beginTransaction();
transaction.begin();
Criteria idNameCrit = session.createCriteria(EmpIdName.class);
empIdNameList = idNameCrit.list();
System.out.println("Records from DatabaseOne");
Iterator idNameIter = empIdNameList.iterator();
while (idNameIter.hasNext()) {
System.out.println();
EmpIdName idNameDb = (EmpIdName) idNameIter.next();
System.out.println("Id From DatabaseOne : "
+ idNameDb.getEmpid());
System.out.println("Name From DatabaseTwo : "
+ idNameDb.getEmpname());
}
session.close();
session = hibernateConfig2.openSession();
Criteria idSalCrit = session.createCriteria(EmpIdSalary.class);
empIdSalaryList = idSalCrit.list();
System.out.println("Records from DatabaseTwo");
Iterator idSalIter = empIdSalaryList.iterator();
while (idSalIter.hasNext()) {
System.out.println();
EmpIdSalary idSalDb = (EmpIdSalary) idSalIter.next();
System.out.println("Id From DatabaseTwo : "
+ idSalDb.getEmpid());
System.out.println("Salary From DatabaseTwo : "
+ idSalDb.getEmpsalary());
}
session.close();
} catch (Exception he) {
he.printStackTrace();
}
}
}
[/java]
Output:
[plain gutter=”false”]
Hibernate:
/* criteria query */ select
this_.EMPID as EMPID0_0_,
this_.EMPNAME as EMPNAME0_0_
from
EMPIDNAME this_
Records from DatabaseOne
Id From DatabaseOne : 100
Name From DatabaseTwo : HEMS
Id From DatabaseOne : 101
Name From DatabaseTwo : RVS
Id From DatabaseOne : 102
Name From DatabaseTwo : PUGAZ
Id From DatabaseOne : 103
Name From DatabaseTwo : GVS
Id From DatabaseOne : 104
Name From DatabaseTwo : CHUDAR
Id From DatabaseOne : 105
Name From DatabaseTwo : ARAVIND
Hibernate:
/* criteria query */ select
this_.EMPID as EMPID2_0_,
this_.EMPSALARY as EMPSALARY2_0_
from
EMPIDSALARY this_
Records from DatabaseTwo
Id From DatabaseTwo : 100
Salary From DatabaseTwo : 6000
Id From DatabaseTwo : 101
Salary From DatabaseTwo : 3000
Id From DatabaseTwo : 102
Salary From DatabaseTwo : 8000
Id From DatabaseTwo : 103
Salary From DatabaseTwo : 4500
Id From DatabaseTwo : 104
Salary From DatabaseTwo : 7000
Id From DatabaseTwo : 105
Salary From DatabaseTwo : 2800
[/plain]
Download Source code:
Two Databases Hibernate (hbm mapping)
Recommended Books: