How to connect to multiple databases in Hibernate

后端 未结 6 797
长发绾君心
长发绾君心 2020-11-27 16:37

I am new bee to Hibernate 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 t
相关标签:
6条回答
  • 2020-11-27 17:06

    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"/>
    
    0 讨论(0)
  • 2020-11-27 17:10

    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.

    0 讨论(0)
  • 2020-11-27 17:16

    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.

    0 讨论(0)
  • 2020-11-27 17:26

    You can also use a catalog with the value of the other database

    @Table(name = "foo", schema = "bar", catalog = "OtherDatabase")

    0 讨论(0)
  • 2020-11-27 17:26

    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 讨论(0)
  • 2020-11-27 17:27

    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()
    
    0 讨论(0)
提交回复
热议问题