java – How to handle multiple banks with Hibernate?

Question:

I'm testing how Hibernate works and I came across a question. I have the following configuration:

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/dbFatura?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>

        <!-- <mapping resource="org/hibernate/tutorial/domain/Fatura.hbm.xml"/> -->
        <mapping class="org.hibernate.tutorial.domain.Fatura"/>

    </session-factory>

</hibernate-configuration>

SessionFactory

private static SessionFactory buildSessionFactory() {
        try{
            return new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation factory failed. " + ex);
            throw  new ExceptionInInitializerError(ex);
        }
    }

In this configuration, I'm determining the bank that I'm going to use in this case, dbFatura :

<property name="connection.url">jdbc:mysql://localhost/dbFatura?useTimezone=true&amp;serverTimezone=UTC</property>

The mapping and manipulation of data works correctly. But, that's where the doubts come in:

  • Is it possible to manipulate multiple banks in the same application? (ex: dbInvoice, dbMovimentacoes)

  • If it is possible, how can I change the bank in the execution of the class?

  • Can I create several .xml files, to move each bank separately from SessionFactory ?

Answer:

An alternative found, to be able to use two banks, is to create two hibernate configuration files and manipulate them according to use:

hibernate-event.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/event?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>

    <mapping class="org.hibernate.Event"/>

    </session-factory>
</hibernate-configuration>

hibernate-client.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/client?useTimezone=true&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>

    <mapping class="org.hibernate.Client"/>

    </session-factory>
</hibernate-configuration>

In the utility class, which handles the Session creation, when the method is called, it receives the name of the file it wants to manipulate and starts the connection according to the file you choose (bank 1 file or bank 2 file ):

HibernateUtil.java

public class HibernateUtil {
    private static SessionFactory sessionFactory;

    private static SessionFactory buildSessionFactory(String configFile) {
        try{
            configFile += ".cfg.xml";
            return new Configuration().configure(configFile).buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation factory failed. " + ex);
            throw  new ExceptionInInitializerError(ex);
        }
    }
}

And finally, in the class that handles database data, for each specific method, it is necessary to pass the file of the database you want to work with.

ManagerDatabase.java

Select all customers:

public List<Cliente> selectAllClients() {
    List<Cliente> result;
    try {
        session = HibernateUtil.getSessionFactory("hibernate-client").openSession();
        tx = session.beginTransaction();

        query = session.createQuery("FROM clients");

        result = query.list();

        tx.commit();
    } catch (HibernateException he) {
        System.out.println("Was found an error in execution!" + he.getCause());
        result = null;
        return result;
    } finally {
        closeSession();
    }
    return result;
}

Select all events:

public List<Cliente> selectAllEvents() {
    List<Cliente> result;
    try {
        session = HibernateUtil.getSessionFactory("hibernate-event").openSession();
        tx = session.beginTransaction();

        query = session.createQuery("FROM events");

        result = query.list();

        tx.commit();
    } catch (HibernateException he) {
        System.out.println("Was found an error in execution!" + he.getCause());
        result = null;
        return result;
    } finally {
        closeSession();
    }
    return result;
}
Scroll to Top