tags:

views:

34

answers:

2

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

+1  A: 

Yes, define multiple persistence units in persistence.xml. Then, when injecting your @PersistenceContext specify the unitName attribute.

Bozho
Yes, that might work but the persistance units are dynamically named and can not be defined in the persistance.xml. I define one class in persitance.xml and like to use that to reach different databases.
Per
A: 

My solution to that was to write a StorageManagerfactory that has a Map holding all EntityManagers

I don't think a Map of EntityManager is a good idea.

However, it should be possible to use Persistence.createEntityManagerFactory(String, Map) to create N EntityManagerFactory pointing 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 these EntityManagerFactory in a Map.

Then, simply get the "right" EntityManagerFactory from the Map (use the whatever key you want) and get an EntityManager.

Pascal Thivent
Yes, that is a good solution, but it does not work for me. This solution means that I have to know all databases and then add them in the persistance.xml-file. That is not the solution I need, because when I start upp the server it will have a lot of configuration parameters and they can point to new databases not used before (this is done dynamically) and therefor it will not work to add them manually in persistance.xml.Thanks for your answer
Per
@Per I'm not sure you did understand my answer nor what the factory method I suggested to check is doing. You don't have to declare all databases in the `persistence.xml` file but only one persistence unit. Then, pass new database "coordinates" as properties in the `Map` of the factory method to override existing values. This gives you the dynamic part that you seem to need.
Pascal Thivent
I thought your solution was promising and I have implemented it as you said, but I still get the same problem. I see that I am using the correct EMF and the output Begin deploying Persistence Unit XXX; state Deployed; factoryCount 2 seems OK, but it still writes all the information in the first factory settings database (factoryCount 1). What can be wrong?
Per
You had right. It works!!!I checked my code again and I made a coding error and that was why your suggested solution did not work. After changing it it work perfect!Thanks for your help.The solution was to:set persistance.createEntityManagerFactory(String, Map) in my datasetter class and then get a specificentityManagerFactory.getEntityManagerFactory();setEntityManager(entityManagerFactory.createEntityManager(Map));in my datastorage class.
Per