How to connect multiple databases with reuse of cfg.xml and util classes?

I want to connect several databases, such as SQL and Oracle, with different databases. So I already had MSSQL hibernate.cfg.xml and the Hibernateutil class for the factory session. Now I am trying to connect to oracle with a different table.

Please advise that I can use the same cgf.xml, and the util class can also tune oracle databases.

This is where the class is used.

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    @SuppressWarnings("deprecation")
    private static SessionFactory buildSessionFactory()
    {
        try
        {

            SessionFactory sessionFactory = new Configuration().configure("/DAO/hibernate.cfg.xml").buildSessionFactory();
            return sessionFactory;        
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}
+4
source share
1 answer

You need to provide cfg.xml for each database.

 Oracle.cfg.xml
 mssql.cfg.xml

, hbm -

SessionFactory .

SessionFactory oracleSF = Configuration.configure("oracle.cfg.xml").buildSessionFfactory();
SessionFactory msSF = Conf..configure("mssql.cfg.xml").build....

util, , SessionFactory .

, hibernate , .

, cfg:

Hibernate

: :

http://www.javabeat.net/hibernate/page/4/

+5

All Articles