Reputation: 2985
Well, I'm working on an application which syncs two databases. I need to setup hibernate.cfg.xml for 2 databases. Postgresql and Firebird. I'd like to create a generic class to set up my hibernate.cfg.xml.
I'd like to do something like this:
currentSessionFactory = new AnnotationConfiguration()
.setProperty("hibernate.dialect", entity.getDialect())
.setProperty("hibernate.connection.driver_class", entity.getDriverClass())
I need a new hibernate.cfg.xml for the second database but I can't set up it via xml, I need to do it through Java code.
How should I do it ?
Upvotes: 2
Views: 3790
Reputation: 153780
You can use Hibernate Configuration class to replace the hibernate.cfg.xml alltogether:
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(Entity1.class);
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
The Configuration class allows you to:
Here you have an example on GitHub.
Upvotes: 3