简体   繁体   English

休眠多个数据库

[英]Hibernate with more than one dataBase

I need to use hibernate with 2 or more bases. 我需要使用具有2个或更多基数的休眠模式。 Since these bases have the same table, I imagine that there will be no problem when using the same objects paras tables that repeat. 由于这些基数具有相同的表,因此我想如果使用相同的重复对象表,则不会有问题。 So being, I wonder how to spend other files on HibernateUtil establishing the connection, using different files hibernate.cfg.xml, but with the same mapping properties. 因此,我想知道如何在HibernateUtil上花费其他文件来建立连接,使用不同的文件hibernate.cfg.xml,但具有相同的映射属性。

I imagine it's something along these lines: 我想这是沿着这些思路:

   sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

but i dont have idea how to configure a other file. 但我不知道如何配置其他文件。

The thing is to have 2 different session factory providers, each one configured with its corresponding data base. 事情是要有2个不同的会话工厂提供程序,每个提供程序都配置有其相应的数据库。

In the example below HibernateUtilDb1 will use the configuration in hibernate-db1.cfg.xml , and HibernateUtilDb2 will use hibernate-db2.cfg.xml . 在下面的示例中, HibernateUtilDb1将使用hibernate-db1.cfg.xml的配置,而HibernateUtilDb2将使用hibernate-db2.cfg.xml

public class HibernateUtilDb1 {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure("hibernate-db1.cfg.xml").buildSessionFactory();
        }
        catch (Throwable ex) { ...}
    }
...

And: 和:

public class HibernateUtilDb2 {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure("hibernate-db2.cfg.xml").buildSessionFactory();
        }
        catch (Throwable ex) { ...}
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM