fredicini
fredicini

Reputation: 11

multiple database with JPA Hibernate without Spring

I have a application with JPA Hibernate without Spring and I need connect to multiple databases (with the same estructure).

The databases will be created for each client, and would have to use the same entity manager. Please I need some idea of ​​how I can do that. I am not using Spring. My application uses MySql, JPA2, Hibernate and JSF.

Thank you!

Upvotes: 0

Views: 646

Answers (1)

Istvan Devai
Istvan Devai

Reputation: 4022

If everything is the same, except for dbname/username/pass, then create a persistence.xml with a persistance unit, put everything there which is static.

Then use the following method to create the entity manager:

javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties);

Supply the variable parameters in the map, like this:

properties.put("hibernate.connection.url", "jdbc:postgresql://127.0.0.1/test");
properties.put("hibernate.connection.username", "joe");
properties.put("hibernate.connection.password", "pass");

Upvotes: 1

Related Questions