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

时间:2009-12-17 13:40:23

标签: hibernate database

我是Hibernate的新宠,并尝试了一些东西。 似乎有趣的一件事是如何连接到不同的数据库? 我在这里有两个问题:

  1. 如果在同一个网络应用程序中我需要连接到MySQL和Oracle,我该怎么做?
  2. 我使用的是MySQL,有两个数据库test1和test2,如何连接和检索数据?
  3. 我在博客中读到我们可以创建不同的配置文件并执行此操作。 我尝试过但不是成功的。 这是我试过的:

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

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

6 个答案:

答案 0 :(得分:40)

使用注释映射作为示例:

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方言和连接信息。

答案 1 :(得分:3)

使用一个hibernate配置文件无法完成。您需要有两个配置文件。

配置mysql数据库

hibernate-mysql.cfg.xml

配置oracle数据库

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>

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

代码应该是这样的。

mysql配置

private static SessionFactory sessionAnnotationFactory; 

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

Session session = sessionAnnotationFactory.openSession();

oracle sql配置

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

Session session = sessionAnnotationFactory.openSession()

答案 2 :(得分:2)

理想情况下,在这种情况下,您应该转移到[使用Java事务分析器org.hibernate.transaction.JTATransactionFactory]的系统的分布式事务类型。如果您在JBoss App Server中运行,则可以使用“分布式事务管理器”来执行此操作。您可以详细了解here

答案 3 :(得分:1)

您还可以将.\bin\manageprofiles.bat -augment -profileName ODM01 -templatePath .\profileTemplates\odm\decisionserver\default -odmHome C:\IBM\ODM892 -databaseConfigFile .\profileTemplates\odm\decisionserver\default\ derbyDBSampleSetting.properties Error:- WASX7357I: By request, this scripting client is not connected to any server process. Certain configuration and application operations will be available in local mode. WASX7303I: The following options are passed to the scripting environment and are available as arguments that are stored in the argv variable: "[C:/IBM/WebSphere/AppServer/profileTemplates/odm/decisionserver/default/actions/scripts, C:/IBM/ODM892, server1]" ERROR: Unable to load resource bundle! (com.ibm.ws.scripting.resources.messages) !GBRPT0053I! ERROR: Unable to load resource bundle! (com.ibm.ws.scripting.resources.messages) !GBRPT0017I! Traceback (most recent call last): File "C:\IBM\WebSphere\AppServer\profileTemplates\odm\decisionserver\default\actions\scripts\decisionserverDeploymentHelper.py", line 93, in installXU ra = AdminConfig.installResourceAdapter(modulePath, nodeName, params) com.ibm.ws.scripting.ScriptingException: com.ibm.ws.scripting.ScriptingException: com.ibm.websphere.management.exception.ConfigServiceException com.ibm.websphere.management.exception.AdminException com.ibm.websphere.management.exception.AdminException java.util.MissingResourceException: java.util.MissingResourceException: Can't find com.ibm.rules.res.xu.messages bundle ERROR: Unable to load resource bundle! (com.ibm.ws.scripting.resources.messages) WASX7017E: Exception received while running file "C:\IBM\WebSphere\AppServer\profileTemplates\odm\decisionserver\default\actions/scripts/installExecutionUnitAdapter.py"; exception information: com.ibm.websphere.management.exception.ConfigServiceException com.ibm.websphere.management.exception.AdminException com.ibm.websphere.management.exception.AdminException java.util.MissingResourceException: java.util.MissingResourceException: Can't find com.ibm.rules.res.xu.messages bundle 与其他数据库的值一起使用

catalog

答案 4 :(得分:0)

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

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

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

答案 5 :(得分:-1)

你可以连接两个数据库test1和test2,只用一个hibernate检索数据有一些技巧:

  • hibernate SQLQuery:只需添加数据库名称和表格#34;选择*来自test1.table1&#34;,&#34; select * from test2.table2&#34;

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

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