Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Hibernate中连接到多个数据库_Hibernate_Database - Fatal编程技术网

如何在Hibernate中连接到多个数据库

如何在Hibernate中连接到多个数据库,hibernate,database,Hibernate,Database,我是新的蜜蜂冬眠和尝试的东西。 有一件事似乎很有趣,那就是如何连接到不同的数据库? 我这里有两个问题: 如果在同一个web应用程序中,我需要连接MySQL和Oracle,我该怎么做 我使用的是MySQL,有两个数据库test1和test2,如何连接和检索数据 我在一篇博客中读到,我们可以创建不同的配置文件并执行它。 我试过了,但没有成功。 以下是我尝试过的: SessionFactory sf = (SessionFactory) new Configuration().configure(pa

我是新的蜜蜂冬眠和尝试的东西。 有一件事似乎很有趣,那就是如何连接到不同的数据库? 我这里有两个问题:

  • 如果在同一个web应用程序中,我需要连接MySQL和Oracle,我该怎么做
  • 我使用的是MySQL,有两个数据库test1和test2,如何连接和检索数据
  • 我在一篇博客中读到,我们可以创建不同的配置文件并执行它。 我试过了,但没有成功。 以下是我尝试过的:

    SessionFactory sf = (SessionFactory) new Configuration().configure(path);
    
    其中path是配置文件的路径。
    这是正确的方法吗?

    在这种情况下,理想情况下您应该转向分布式事务类型的系统[使用Java事务分析器org.hibernate.transaction.JTATransactionFactory]。如果您在JBoss App Server中运行,则可以使用“分布式事务管理器”来实现。您可以了解更多信息。

    以注释映射为例:

    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();
    

    然后使用sf1和sf2获取每个数据库的会话。对于映射文件,只需使用cfg.addClass而不是addAnnotatedClass。在本例中,将cfg.xml文件放入根包中。这些将具有Oracle或MySQL方言和连接信息。

    您可以连接两个数据库test1和test2,只需一个hibernate即可检索数据,并使用一些技巧:

    • HibernateSqlQuery:只需在表“select*fromtest1.table1”、“select*fromtest2.table2”中添加数据库名称

    • hibernate持久性:在hibernate映射xml中使用密钥模式

      
      


    使用一个hibernate配置文件无法完成此操作。你需要有两个配置文件

    配置
    mysql
    数据库

    hibernate-mysql.cfg.xml
    
    hibernate-oracle.cfg.xml
    
    配置oracle数据库的步骤

    hibernate-mysql.cfg.xml
    
    hibernate-oracle.cfg.xml
    
    具体来说,
    mysql
    配置文件应该是这样的

    <?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>
    
    <?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>
    
    代码应该是这样的

    <?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>
    
    <?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>
    
    mysql配置

    private static SessionFactory sessionAnnotationFactory; 
    
    sessionAnnotationFactory = new Configuration().configure("hibernate-mysql.cfg.xml").buildSessionFactory();
    
    Session session = sessionAnnotationFactory.openSession();
    
    sessionAnnotationFactory = new Configuration().configure("hibernate-oracle.cfg.xml").buildSessionFactory();
    
    Session session = sessionAnnotationFactory.openSession()
    
    oracle sql配置

    private static SessionFactory sessionAnnotationFactory; 
    
    sessionAnnotationFactory = new Configuration().configure("hibernate-mysql.cfg.xml").buildSessionFactory();
    
    Session session = sessionAnnotationFactory.openSession();
    
    sessionAnnotationFactory = new Configuration().configure("hibernate-oracle.cfg.xml").buildSessionFactory();
    
    Session session = sessionAnnotationFactory.openSession()
    

    还可以在configuration.xml文件中添加映射类

    注意:这是用于注释和资源的,请使用resources关键字而不是class

    <mapping class="packageName.classNmae1"/>
    <mapping class="packageName.classNmae2"/>
    

    您还可以将
    目录
    与其他数据库的值一起使用


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

    您是否在使用spring,如果是这样,这将变得更容易是的,我正在使用springs框架。如果你能为我提供一个有效的例子,那就太好了。非常感谢。这正是我想要的。所以我们需要多个会话工厂实例来配置多个数据库。不同的scheema不是不同的数据库。