Hibernate Multiple Databases: Example
Hibernate Multiple Databases: Example
Description
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.
If multiple configuration files are created then we need multiple SessionFactory objects.
For each Session Factory Object aSession is needed and transaction must begin within that
Session object.
Example
Step-1
Create one application, which insert the employee details in
two databases,Oracle & MySQL. Create the project directory structure first.
Step-2
Create the persistence class.
SpLessons.java
package com.itoolsinfo;
}
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.
Step-3
To map the persistence class in mapping file.
splessons.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hib
<hibernate-mapping>
<class name="com.iquickinfo.Splessons" table="splessons">
<id name="empId" column="eid">
<generator class="assigned"/>
</id>
Step-4
Configure the mapping file in Configuration file.
oracle.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<hibernate-configuration>
<session-factory>
<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"/>
</session-factory>
</hibernate-configuration>
mysql.cfg.xml
<hibernate-configuration>
<session-factory>
<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"/>
</session-factory>
</hibernate-configuration>
Properties Description
Step-5
Create the employee class and stores the persistence class object.
Employess.java
package com.iquickinfo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
SessionFactory factory1=configuration1.buildSessionFactory();
SessionFactory factory2=configuration2.buildSessionFactory();
Session oraclesession=factory1.openSession();
Session mysqlsession=factory2.openSession();
Transaction transaction1=oraclesession.beginTransaction();
oraclesession.save(splessons);
transaction1.commit();
Transaction transaction2=mysqlsession.beginTransaction();
mysqlsession.save(splessons);
transaction2.commit();
oraclesession.close();
mysqlsession.close();
factory1.close();
factory2.close();
}
}
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.
Step-6
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.
Step-7
Also enter the values into the database like the following.
Output:
Summary
Key Points
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.
Please install following freeware softwares in my system -Citix -JDK1.8 64 bit -Eclipse Luna 64 bit, environment variable
and CISCO VPN