Hibernate accesses multiple databases

Source: Internet
Author: User
Tags relational database table

I. Loading Process when Hibernate accesses the database

Most friends who use Hibernate usually use the following method to obtain the Configuration instance: Configuration configure = new Configuration (). configure ();

In Hibernate, Configuration is the entry of hibernate. When instantiating a Configuration, Hibernate will automatically search for the Hibernate Configuration file hibernate. properties in the environment variable (classpath. If the file exists, the content of the file is loaded into the GLOBAL_PROPERTIES instance of a Properties. If the file does not exist, the information hibernate. properties not found is printed;

Next, Hibernate adds all System environment variables (System. getProperties () to GLOBAL_PROPERTIES. If the configuration file hibernate. properties exists, the system will further verify the configuration validity of this file. For some unsupported configuration parameters, the system will print the warning information.

By default, the configure () method will automatically search for the Hibernate configuration file hibernate under the environment variable (classpath. cfg. xml. If the file does not exist, the system prints the following information and throws the HibernateException: hibernate. cfg. xml not found; if the file exists, the configure () method first accesses <session-factory> and obtains the attribute of the element name. If the attribute of name is not empty, this configuration value will be used to overwrite hibernate. properties hibernate. the Configuration value of session_factory_name. here we can see that hibernate. cfg. the configuration information in xml can overwrite hibernate. properties configuration information.

Next, the configure () method accesses the sub-elements of <session-factory>. First, all the <property> element configuration information will be used to overwrite the corresponding configuration information in hibernate. properties.
Configure () Then accesses the content of the following elements in sequence.

<mapping>
<jcs-class-cache>
<jcs-collection-cache>
<collection-cache>


<Mapping> is essential. You must configure <mapping> and configure () to access the ing file (hbm) of the defined java object and relational database table. xml), for example:
<Mapping resource = "Cat. hbm. xml"/>

In this way, the configure () method creates a Configuration instance using various resources. For the entire project, if a local thread is used to store the Configuration instance, the entire project only needs to instantiate the Configuration object once (note: the Configuration instance takes a long time ), this improves the project efficiency.

2. Configure Hibernate to access multiple databases
As described above, the configure () method loads the. hbm. xml file by accessing the <mapping> element of hibernate. cfg. xml by default. We can also directly specify hbm. xml file. For example, the addClass () method can directly load the corresponding ing file by specifying the class. hibernate will automatically convert the full name of the provided class (including package) to the file path, you can also use the addFile method to directly specify the ing file. For example:
Configuration reset ate = new Configuration (). addClass ("Book. class ");
Configuration configurate = new Configuration().addURL(Configuration.class.getResource ("/Book.hbm.xml"));
Configuration config = new Configuration().addFile("/Cat.hbm.xml");


In this way, if you use xml configuration to configure multiple databases, write multiple configuration files. Suppose there are two databases (MySQL and SQLServer). We can define the xml file as "mysql. cfg. xml" and "sqlserver. cfg. xml ". The code for getting SessionFactory using the Configuration class is as follows:
SessionFactory mysqlFactory = new Configuration().configure("/mysql.cfg.xml").buildSessionFactory(); 
SessionFactory sqlserverFactory = new Configuration().configure("sqlserver.cfg.xml ").buildSessionFactory();


If you use spring, it is easier to use multiple databases. For example, this code can complete all the configurations:
  
  

jdbc:mysql://localhost:3306/test

org.gjt.mm.mysql.Driver

root

123





org.hibernate.dialect.MySQLDialect



test.hbm.xml





jdbc:odbc:test

sun.jdbc.odbc.JdbcOdbcDriver

root

123





org.hibernate.dialect.SQLServerDialect



test.hbm.xml



.......


The above is only a general method for configuring Hibernate to access multiple databases. There are many feasible configurations for hibernate. Interested readers can refer to its reference.

About Author:
YANG Xiao, master of computer science from Beijing Business University, once worked in java Development in NEC-SI and Softbrain.
Blog: http://blog.sina.com.cn/u/1237288325

Related Article

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.