0% found this document useful (0 votes)
53 views

Hibernate Multiple Databases: Example

Hibernate can connect to multiple databases by creating separate configuration files and SessionFactory objects for each database. Each configuration file specifies the connection details like driver, URL, username, password etc. for a single database. A separate Session is created from each SessionFactory and transactions are managed within each Session to perform CRUD operations on the respective database. This allows an application to save and retrieve data from multiple databases using Hibernate.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Hibernate Multiple Databases: Example

Hibernate can connect to multiple databases by creating separate configuration files and SessionFactory objects for each database. Each configuration file specifies the connection details like driver, URL, username, password etc. for a single database. A separate Session is created from each SessionFactory and transactions are managed within each Session to perform CRUD operations on the respective database. This allows an application to save and retrieve data from multiple databases using Hibernate.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Hibernate Multiple Databases

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;

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.

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>

<property name="empName" column="ename"/>


<property name="city" column="city"/>
</class>
</hibernate-mapping>
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.

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

<?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">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

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.

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;

public class Employees


{
public static void main(String[] args)
{

Configuration configuration1=new Configuration();


configuration1.configure("oracle.cfg.xml");

Configuration configuration2=new Configuration();


configuration2.configure("mysql.cfg.xml");

SessionFactory factory1=configuration1.buildSessionFactory();
SessionFactory factory2=configuration2.buildSessionFactory();

Session oraclesession=factory1.openSession();
Session mysqlsession=factory2.openSession();

Splessons splessons=new Splessons();


splessons.setEmpId(414);
splessons.setEmpName(" John ");
splessons.setCity("Hyderabad");

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:

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

select * from splessons;


Output will be as follows with following column names.

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

Required for development and connecting to client

You might also like