Hibernate - SPLessons

Chapter 10

Hibernate Multiple Databases

Hibernate Multiple Databases

While dealing with big projects hibernate may need more databases then more configuration files will be needed. The driver names also be changed because of every database will consists of own driver names. Dialects also will be changed. Following are the some key points while dealing with hibernate multiple databases.


Create one application, which insert the employee details in two databases, Oracle & MySQL. Create the project directory structure first.

Create the persistence class.


package com.itoolsinfo;

public class SpLessons 
	private int empId;
	private String empName;
	private String city;
	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 String getCity() {
		return city;
	public void setCity(String city) {
		this.city = city;


Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.

To map the persistence class in mapping file.

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
    <class name="com.iquickinfo.Splessons" table="splessons">
      <id name="empId" column="eid">
         <generator class="assigned"/>
      <property name="empName" column="ename"/>
     <property name="city" column="city"/>

The generator classsubelement of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the org.hibernate.id.IdentifierGenerator interface.

Configure the mapping file in Configuration file.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
          <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
          <property name="connection.username">root</property> 
         <property name="connection.password">root</property> 
         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 
        <property name="hibernate.hbm2ddl.auto">create</property>
         <property name="show_sql">true</property>
         <mapping resource="splessons.hbm.xml"/>



 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
         <property name="connection.url">jdbc:mysql://localhost:3306/MySQL</property>
         <property name="connection.username">system</property>  
         <property name="connection.password">system</property> 
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         <property name="hibernate.hbm2ddl.auto">create</property>
         <property name="show_sql">true</property>
         <mapping resource="splessons.hbm.xml"/>
Properties Description
hibernate.connection.driver_class The JDBC driver class.
hibernate.dialect This property makes Hibernate generate the suitable SQL for the picked database.
hibernate.connection.url The JDBC URL to the database instance.
hibernate.connection.username The database username.
hibernate.connection.password The database password.
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool.
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection.

Create the employee class and stores the persistence class object.


package com.iquickinfo;

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

public class Employees 
	public static void main(String[] args)
	    Configuration configuration1=new Configuration();
	    Configuration configuration2=new Configuration();
	    SessionFactory factory1=configuration1.buildSessionFactory(); 
	    SessionFactory factory2=configuration2.buildSessionFactory();
	      Session oraclesession=factory1.openSession();
	      Session mysqlsession=factory2.openSession();
	        Splessons splessons=new Splessons();
	           splessons.setEmpName("  John   ");
	      Transaction transaction1=oraclesession.beginTransaction();
	      Transaction transaction2=mysqlsession.beginTransaction();

SessionFactory is an interface, which is accessible in “org.hibernate” package.Session factory is long live multithreaded object. Typically one session manufacturing plant ought to be made for one database. When the developer have different databases in your application, developer ought to make numerous SessionFactory object.

Add all the jar files in build path apart from ojdbc14.jar, mysql-connector-5.2.jar and Hibernate jar files.Run the java application and see the output in command prompt.
Also enter the values into the database like the following.


Output can be seen in Database table by using the following command.

select * from splessons;

Output will be as follows with following column names.


  • Hibernate Multiple Databases – While using more databases, then more configuration files are needed.
  • Hibernate Multiple Databases – One configuration file will have one database information like driver name.
  • Hibernate Multiple Databases – Each database will have user name and password that should be mention in each configuration file.