Hibernate How to configure the operation of multiple databases ___ database

Source: Internet
Author: User

Hibernate how to configure operations on multiple databases

Question:

Reference:
I'm working on a project that needs to access data from two database instances, and how to configure and process it in a hibernate implementation

--------------------------------------------------------------------------------

1. If a project is going to read data from two or more database instances, how to handle it.
2. If there is only one database Oraa, establish oraA.hbm.xml and hibernate.properties files for it, the database connection part of the file is written as:
# # Oracle
Hibernate.dialect Net.sf.hibernate.dialect.OracleDialect
Hibernate.connection.driver_class Oracle.jdbc.driver.OracleDriver
Hibernate.connection.username username
Hibernate.connection.password Password
Hibernate.connection.url Jdbc:oracle:thin: @local: 1521:ora

3, if there are two Oracle database Oraa and Orab, Create OraA.hbm.xml and OraB.hbm.xml mapping files for each, so that each table and entity persistence class in the database can be mapped, but how do you handle the database configuration part of hibernate.properties, and how can you connect two database instances at the same time?


If you use this configuration.configure (File configfile)
ConfigFile can be different configurations.

The configuration of each database can be obtained by configuring configfile, which means that the session of each database instance can be obtained , but in the hibernate.properties file how to write the database connection section, a database when the writing is: Hibernate.connection.url Jdbc:oracle:thin: @local: 1521:ora

If there are two database instances, how to deal with the Hibernate.properties file, it will not be written as follows:
##oracle
Hibernate.dialect Net.sf.hibernate.dialect.OracleDialect
Hibernate.connection.driver_class Oracle.jdbc.driver.OracleDriver
Hibernate.connection.username username
Hibernate.connection.password Password
Hibernate.connection.url Jdbc:oracle:thin: @local: 1521:oraa
Hibernate.connection.username Usernamet
Hibernate.connection.password Passwordt
Hibernate.connection.url Jdbc:oracle:thin: @local: 1521:orab


Answer:

Write two hibernate.properties, such as the connection Oraa is called hibernatea.properties, the connection Orab is called Hibernateb.properties.

Hibernatea.properties contents are as follows:

Reference:
Hibernate.dialect Net.sf.hibernate.dialect.OracleDialect
Hibernate.connection.driver_class Oracle.jdbc.driver.OracleDriver
Hibernate.connection.username username
Hibernate.connection.password Password
Hibernate.connection.url Jdbc:oracle:thin: @local: 1521:oraa



Hibernateb.properties contents are as follows:

Reference:
Hibernate.dialect Net.sf.hibernate.dialect.OracleDialect
Hibernate.connection.driver_class Oracle.jdbc.driver.OracleDriver
Hibernate.connection.username Usernamet
Hibernate.connection.password Passwordt
Hibernate.connection.url Jdbc:oracle:thin: @local: 1521:orab


Program Inside:

Code:
Java code:

Configuration Conf_oraa = new Configuration ("/hibernatea.properties"). AddClass ().

Configuration Conf_orab = new Configuration ("/hibernateb.properties"). AddClass ().

Sessionfactory Sf_oraa = Conf_oraa.buildsessionfactory ();
Sessionfactory Sf_orab = Conf_orab.buildsessionfactory ();

Session S_oraa = Sf_oraa.opensession ();
Session S_orab = Sf_orab.opensession ();

......


---------------------------------------------------------------

Correct
Programme I:
Write two Hibernate.cfg.xml
When using Database A
Configuration cfg_a=new Configuration (). Configure (Hibernate.cfg_a.xml);
When using database B,
Configuration cfg_b=new Configuration (). Configure (Hibernate.cfg_b.xml);

Usr/home/zk1007874/htdocs/upload_files/images



Programme II:
Using an account that can operate two databases, in the?. The class tag in the hbm.xml mapping file specifies catalog as the database name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.