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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1921865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:33:04  来源:igfitidea点击:

How to connect to multiple databases in Hibernate

hibernatedatabase

提问by akellakarthik

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:

我是 Hibernate 的新手,正在尝试各种东西。似乎让所有人感到有趣的一件事是如何连接到不同的数据库?我在这里有两个问题:

  1. If in the same web app I need to connect to MySQL and Oracle, how do I do it?
  2. I am using MySQL and have two databases test1 and test2, how to connect and retrieve data?
  1. 如果在同一个 Web 应用程序中我需要连接到 MySQL 和 Oracle,我该怎么做?
  2. 我正在使用 MySQL 并且有两个数据库 test1 和 test2,如何连接和检索数据?

I have read in a blog that we can create different configuration files and do it. I tried it but was not sucessful. Here's what I tried:

我在博客中读到我们可以创建不同的配置文件并执行它。我试过了,但没有成功。这是我尝试过的:

SessionFactory sf = (SessionFactory) new Configuration().configure(path);

Where path is the path of the config file. Is this the right way?

其中 path 是配置文件的路径。这是正确的方法吗?

回答by Brian Deterling

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.

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

回答by Dulith De Costa

It cannot be done using one hibernate configuration file. You need to have two configurations files for it.

它不能使用一个休眠配置文件来完成。你需要有两个配置文件。

To configure mysqldatabase

配置mysql数据库

hibernate-mysql.cfg.xml

To configure oracledatabase

配置oracle数据库

hibernate-oracle.cfg.xml

In Details, mysqlconfiguration file be like this.

在详细信息中,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>

In Details, oracleconfiguration file be like this.

在详细信息中,oracle配置文件是这样的。

<?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

mysql配置

private static SessionFactory sessionAnnotationFactory; 

sessionAnnotationFactory = new Configuration().configure("hibernate-mysql.cfg.xml").buildSessionFactory();

Session session = sessionAnnotationFactory.openSession();

oracle sql configuration

oracle sql配置

sessionAnnotationFactory = new Configuration().configure("hibernate-oracle.cfg.xml").buildSessionFactory();

Session session = sessionAnnotationFactory.openSession()

回答by Shamik

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.

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

回答by Sam Barnum

You can also use a catalogwith the value of the other database

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

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

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

回答by karan

You can also Add mapping class in configuration.xml file

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

Note : this is for annotations and for resources use resources keyword instead of class

注意:这是用于注释和资源使用资源关键字而不是类

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

回答by MashiMaro

You can connect two databases test1 and test2, retrieve data with only one hibernate with some tricks:

你可以连接两个数据库 test1 和 test2,通过一些技巧只用一个休眠来检索数据:

  • 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">

  • Hibernate SQLQuery:只需在表“select * from test1.table1”、“select * from test2.table2”中添加数据库名称

  • hibernate 持久化:使用 hibernate 映射 xml 中的关键模式

    <class name="Table1Class" table="table1" schema="test1"> <class name="Table2Class" table="table2" schema="test2">