Yeah, I got!
If anyone is with the same doubt, I will share here my solution:
Besides Hibernate.cfg.xml, I created another file that I called hibernate1.cfg.xml in which I include the sessionfactory of the other database.
NOTE: In my project, the first Hibernate.cfg.xml is for Mysql and, the second, for Postgresql.
In the package br.com.meuprojeto.util I already had the class Hibernateutil.java. So I created another class, this time Hibernateutil1.java whose implementation follows:
import org.hibernate.Sessionfactory;
import org.hibernate.boot.registry.Standardserviceregistrybuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Serviceregistry;
public class Hibernateutil1 {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Cria SessionFactory a partir do hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate1.cfg.xml").buildSessionFactory();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Falha ao tentar criar o SessionFactory." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
- Within my package br.com.meuprojeto.main, I have the class Geratable.java with the main method calling the two classes Hibernateutil and Hibernateutil1, as follows:
public class Geratabela {
public static void main(String[] args) {
HibernateUtil.getSessionFactory();
HibernateUtil.getSessionFactory().close();
HibernateUtil1.getSessionFactory();
HibernateUtil1.getSessionFactory().close();
}
}
It may not be the most elegant solution. But I ran this class and there were the tables created in both banks.
Thanks to those who accompanied!