How to connect to multiple databases in Hibernate

I am new bee to Hibernate and trying out things. One thing that seems to amuse all is how to connect to different databases? I have two questions here:

  1. If in the same web app I need to connect to MySQL and Oracle, how do I do it?
  2. I am using MySQL and have two databases test1 and test2, how to connect and retrieve data?

I have read in a blog that we can create different configuration files and do it. I tried it but was not sucessful. Here's what I tried:

SessionFactory sf = (SessionFactory) new Configuration().configure(path);

Where path is the path of the config file. Is this the right way?


Using annotation mappings as an example:

Configuration cfg1 = new AnnotationConfiguration();
cfg1.addAnnotatedClass(SomeClass.class); // mapped classes
SessionFactory sf1 = cfg1.buildSessionFactory();

Configuration cfg2 = new AnnotationConfiguration();
cfg2.addAnnotatedClass(SomeClass.class); // could be the same or different than above
SessionFactory sf2 = cfg2.buildSessionFactory();

Then use sf1 and sf2 to get the sessions for each database. For mapping files, you just use cfg.addClass instead of addAnnotatedClass. Put the cfg.xml files in the root package in this case. Those will have the Oracle or MySQL dialect and connection information.

It cannot be done using one hibernate configuration file. You need to have two configurations files for it.

To configure mysql database


To configure oracle database


In Details, mysql configuration file be like this.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">PASSWORD</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/UR_DB_NAME</property>
        <property name="hibernate.connection.username">USERNAME</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping class="domain.EmployeeMysql"></mapping>

In Details, oracle configuration file be like this.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">PASSWORD</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:UR DB NAME</property>
        <property name="hibernate.connection.username">USERNAME</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="show_sql">true</property>
        <mapping class="domain.EmployeeOracleSql"></mapping>

And code should be like this.

mysql configuration

private static SessionFactory sessionAnnotationFactory; 

sessionAnnotationFactory = new Configuration().configure("hibernate-mysql.cfg.xml").buildSessionFactory();

Session session = sessionAnnotationFactory.openSession();

oracle sql configuration

sessionAnnotationFactory = new Configuration().configure("hibernate-oracle.cfg.xml").buildSessionFactory();

Session session = sessionAnnotationFactory.openSession()

Ideally you should move to Distributed transaction type of system[using Java Transaction Analyzer org.hibernate.transaction.JTATransactionFactory] in this case. If you are running in JBoss App Server, you can do it by using "Distributed Transaction Managers". You can learn more about it here.

You can also use a catalog with the value of the other database

@Table(name = "foo", schema = "bar", catalog = "OtherDatabase")

You can also Add mapping class in configuration.xml file

Note : this is for annotations and for resources use resources keyword instead of class

<mapping class="packageName.classNmae1"/>
<mapping class="packageName.classNmae2"/>

You can connect two databases test1 and test2, retrieve data with only one hibernate with some tricks:

  • hibernate SQLQuery: just add database name with the table "select * from test1.table1", "select * from test2.table2"

  • hibernate persistence: using the key schema in the hibernate mapping xml

    <class name="Table1Class" table="table1" schema="test1"> <class name="Table2Class" table="table2" schema="test2">

Need Your Help

How to ajax-submit a form textarea input from CKEditor?

javascript jquery ckeditor html-form jquery-forms-plugin

I am using CKEditor, jQuery and jQuery form plugin and I would like to submit contents of the CkEditor form via an Ajax query. Here is my code:

Efficiently merge string arrays in .NET, keeping distinct values

c# linq arrays merge distinct

I'm using .NET 3.5. I have two string arrays, which may share one or more values: