I am using Java persistance and there EntityManager class and have it assigned to storage a class object that shall be written to the database. My problem is that I want to write to different databases using the same storage class. My solution to that was to write a StorageManagerfactory that has a Map holding all EntityManagers. The solution looked good until I looked at the databases and realized that all information (undepending of the Map, which gets the correct value) was written to the same database (one of the initialised in the Map).
So my question is:
Can I write to different databases using JPA that is using the same storage class (the class holding the structure of my database)?
Thanks
I don’t think a
MapofEntityManageris a good idea.However, it should be possible to use
Persistence.createEntityManagerFactory(String, Map) to create NEntityManagerFactorypointing to N databases (the mentioned factory method allows to pass additional properties that override values that may have been configured elsewhere, for example a connection url) and to theseEntityManagerFactoryin aMap.Then, simply get the “right”
EntityManagerFactoryfrom theMap(use the whatever key you want) and get anEntityManager.