开发者

How to connect to multiple databases in Hibernate

I am new bee to Hibernat开发者_高级运维e 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.configure("/hibernate-oracle.cfg.xml");
cfg1.addAnnotatedClass(SomeClass.class); // mapped classes
cfg1.addAnnotatedClass(SomeOtherClass.class);
SessionFactory sf1 = cfg1.buildSessionFactory();

Configuration cfg2 = new AnnotationConfiguration();
cfg2.configure("/hibernate-mysql.cfg.xml");
cfg2.addAnnotatedClass(SomeClass.class); // could be the same or different than above
cfg2.addAnnotatedClass(SomeOtherClass.class);
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

hibernate-mysql.cfg.xml

To configure oracle database

hibernate-oracle.cfg.xml

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"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <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>
    </session-factory>
</hibernate-configuration>

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"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <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>
    </session-factory>
</hibernate-configuration>

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜