Hibernate connection accesses multiple databases (with the same table that accesses different databases)

Source: Internet
Author: User
Tags db2

Use Hibernate to access different tables in different databases or the same tables in different databases.

I in the development process of the solution, I hope you exchange.
The general use of the MyEclipse tool will automatically generate hibernate related files, roughly the following categories:

(1) Database configuration file:. cfg.xml
(2) Mapping file. Hbm.xml
(3) Mapping class: Pojo
(4) Session factory class: Sessionfactory
(5) Basic DAO Interface: Ibasehibernatedao
(6) DAO interface Implementation Basic class: Basehibernatedao
(7) Data Access object: DAO. All DAO default Inheritance Basehibernatedao

Of course, the files generated by different tools are slightly different, but do not affect the implementation of ideas.


The generic tool generates a configuration file for only one database, so a database profile is generated.
Here I have access to the same tables in different databases for example, access to different database different table principle similar.
Suppose there are two databases DB1 and DB2, two libraries have the user table, to achieve the table access in two databases, the operation is as follows:

(1) Configure two database configuration files: Db1.cfg.xml and db2.cfg.xml, connecting two databases respectively.
(2) Configure two SESSIONFACTORY:SESSIONFACTORY_DB1 bindings db1.cfg.xml,sessionfactory_db2, bind Db2.cfg.xml.
(3) Basic DAO Interface: Ibasehibernatedao remains unchanged, code defaults as follows
Public interface Ibasehibernatedao {
Public Session getsession ();

}

(4) DAO interface implementation Basic class: Basehibernatedao after the transformation as follows:


public class Basehibernatedao Implementsibasehibernatedao {

Private String dbname;//The database to connect to

To ensure that each DAO can correctly specify the database to be operated on, the parameterless construct is set to private
Private Basehibernatedao () {}

To specify the DAO construction method for the target database
Public Basehibernatedao (Stringdbname) {
This.dbname = DbName;
}

Overriding the GetSession () method so that it can access different databases
Public Session getsession () {
if (DbName = = null) {
return null;
}else if (dbname.equals ("Db01")) {
Returnsessionfactory_db01.getsession ();//Connect DB01 Library
} else if (Dbname.equals ("DB02")) {
Returnsessionfactory_db02.getsession ();//Connect DB02 Library
} else {
return null;
}
}
}

(5) Transform the Dao--userdao class that automatically generates the user table: The Basehibernatedao that inherits the parent class.

public class Userdaoextends Basehibernatedao {

Because the parent class sets the parameterless construct to private, the class can only have a parameter construct.
Public Userinfotbdao (Stringdbname) {
Super (DbName);
}

The following are the auto-generated code:
public void Save (USERTB transientinstance) {
try {
GetSession (). Save (Transientinstance);
Log.debug ("Save Successful");
} catch (RuntimeException re) {
Log.error ("Save Failed", re);
throw re;
}
}
}

(6) Test of application layer

Public Voidtestsaveuser (USERTB user) {

Userdao userdao_1 = Newuserdao ("db01");//DAO that gets DB01 library
Userdao userdao_2 = new Userdao ("DB02");//Get DB02 Library of DAO

Assume that data in two libraries is updated synchronously (transaction control should be included in the actual operation)
Userdao_1.save ();//Update 01 Library
Userdao_2.save ();//Update 02 Library

}

(7) Summary:
Advantages:
1. Code churn is small and can take full advantage of tool-generated code.
2. Simple structure, flexible access.
3. Access the same table in different libraries, just one DAO, one pojo, no need to write extra code.

Disadvantages:
1. Specify the database to access each time you connect to the database.
2. In order to effectively specify the logical name of the database, and to facilitate maintenance, the "DB01" string is set to the global final variable, or

A DAO factory to produce different DAO instances.

Another way:
The method above is a modification of the DAO construction method. The GetSession () method can also be modified to be more transparent, but slightly less flexible.

I just contact hibernate, I do not know whether the above design is appropriate, looking master pointing twos.

Hibernate connection access to multiple databases (with the same table accessing different databases) (RPM)

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.