Multiple databases using hibernate annotations

Database and Table Structure:

DATABASE : DATABASEONE

Table: EMPIDNAME

EMPID INT(4)
EMPNAME VARCHAR(15)

DATABASE : DATABASETWO

Table : EMPIDSALARY

EMPID INT (4)
EMPSALARY INT(4)

Project Structure:
hibernate annotation project structure for two databases

Step 1: Hibernate configuration files for both databases:

first database (DATABASEONE) hibernate configuration file (hibernate1.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>
		<!-- 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 class="EmpIdName" />

	</session-factory>

</hibernate-configuration>

second database (DATABASETWO) hibernate configuration file(hibernate2.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>
		<!-- 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 class="EmpIdSalary" />

	</session-factory>

</hibernate-configuration>

Step 2: Class files for the above created tables to map in the hibernate mapping file below (step3),

EmpIdName.java (with annotations):

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPIDNAME")
public class EmpIdName {

	@Id
	@GeneratedValue
	@Column(name = "EMPID")
	private int empid;

	@Column(name = "EMPNAME")
	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;
	}

}

EmpIdSalary.java (with annotations):

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPIDSALARY")
public class EmpIdSalary {

	@Id
	@GeneratedValue
	@Column(name = "EMPID")
	private int empid;

	@Column(name = "EMPSALARY")
	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;
	}

}

Step 3: Main class to retrive the data’s,
EmpIdNameSalary.java:

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.AnnotationConfiguration;

public class EmpIdNameSalary {
	public static void main(String[] args) {
		SessionFactory hibernateConfig1 = new AnnotationConfiguration()
				.configure("hibernate1.cfg.xml").buildSessionFactory();
		SessionFactory hibernateConfig2 = new AnnotationConfiguration()
				.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();
		}
	}

}

Output:

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

Download Source code:

TwoDatabasesHibernate annotation

Recommended Books:

159 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *