Hibernate connection Operation two databases

Source: Internet
Author: User

One, (under SRC) write two Hibernate.cfg.xml files:
            Hbn-mysql.cfg.xml and Hbn-sqlserver.cfg.xml
II, respectively, to resolve the above two. cfg.xml file built two sessionfactory,
three, Which database to access when using session, which sessionfactory open session.

Detailed steps:-----------------------------------------
One, (under SRC) two Hibernate.cfg.xml files

(1.) Content of Hbn-mysql.cfg.xml:

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration dtd//en" "http://
Hibernate.sourceforge.net/hibernate-configuration-2.0.dtd > <!--hibernate configuration file that defines the configuration information used for the MySQL database-->  


(2.) hbn-sqlserver.cfg.xml content:

<?xml version= "1.0"?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http:// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">  


III. testing: Using Getsessionbydb (String dbname) to get a session of different databases

Package com.fourstar.starTransport.dao.util;

Import org.hibernate.HibernateException;
Import Org.hibernate.Query;
Import org.hibernate.Session;

public class test1{public
static void Main (string[] args) {
   String dbname = "MySQL";
String dbname = "Sqlservrer";
   Session s = Hbnutil.getsessionbydb (dbname);
   try { 
    String sql = ' from Freightcompany ';
    Query query = s.createquery (SQL); 
   } catch (Hibernateexception e) {
    e.printstacktrace ();
   } finally{
    s.close ();
    Hbnutil.closesessionfactorybydb (dbname);}}


————————————————————————————————————————

————————————————————————————————————————

after that, I'll introduce you to the principle of hibernate connecting two databases:
——————————————————————————————————————

Hibernate the process of loading when accessing a database

For most friends who use hibernate, it's often a way to get Configuration instances: Configuration
Configure = new Configuration (). Configure ();

In hibernate, configuration is the entrance to hibernate. When instantiating a configuration, Hibernate automatically looks for hibernate profile hibernate.properties inside the environment variable (CLASSPATH). If the file exists, the contents of the file are loaded into an instance of properties global_properties, and if not, the information is printed
Hibernate.properties not found;

Next hibernate adds all the system environment variables (system.getproperties ()) to the global_properties. If the configuration file Hibernate.properties exists, the system will further verify the validity of this file configuration, and the system will print out a warning message for some configuration parameters that are not already supported.


Default state Configure () Method automatically looks for the hibernate configuration file Hibernate.cfg.xml under the environment variable (CLASSPATH), and if the file does not exist, the system prints the following information and throws the Hibernateexception exception:
Hibernate.cfg.xml not
Found if the file exists, the Configure () method first accesses the <session-factory> and gets the property of the element name, if the property of name is not empty. The value of this configuration will be used to overwrite the values of Hibernate.properties's hibernate.session_factory_name configuration, from where we can see that The configuration information inside the hibernate.cfg.xml can overwrite the hibernate.properties configuration information.


Next, the Configure () method accesses the child elements of <session-factory>, first using all the <property> element configuration information to overwrite the corresponding configuration information in the hibernate.properties.

Configure () then accesses the contents of the following elements in turn
<mapping> <jcs-class-cache> <jcs-collection-cache> <collection-cache>

Where <mapping> is essential and must be configured <mapping>, configure () to access the mapping file (hbm.xml) of our defined Java objects and relational database tables, for example:

<mapping resource= "Cat.hbm.xml"/>

This configure () method creates a configuration instance using a variety of resources. For the entire project, if you use a local thread to store this configuration instance, the entire project needs to instantiate only one configuration object (Note: The configuration instance takes time) and increases the efficiency of the project.

The above understanding of parsing hibernate.cfg.xml mapping file makes Configuration Object Instantiation method New Configuration (). Configure (). If we need to connect two databases, we need to parse two hibernate mapping files to instantiate two Configuration objects, we use the method new Configuration (). Configure ("Hibernate file path"). Then use the Configuration object's Buildsessionfactory () method to create the session factory. With the session factory can produce database operation object session.

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.