How to save the same JPA object in multiple databases (distributed system)?

(How) is it possible to save a JPA object in the databases of several servers without copying everything to the DTO?

We have a distributed system. Some applications have caching databases. The JPA provider throws an exception in which it complains that it cannot save a separate object.

But I would like to save the identifier of the object by simply storing it in this additional DB.

(JPA 1.2, EJB 3.0, Glassfish v2.1, Toplink Essentials)

+3
source share
1 answer

em.persist(obj), em.merge(obj). , .

, EntityManager. , , , .

, , ( ), , , EM EM. , , , , , .

.

YourEntity unattachedEntity = ... // your original entity object.

YourEntity managedEntity = em1.merge(unattachedEntity);

// managedEntity now has the primary key assigned by the DB
unattacheEntity.setPrimaryKey(managedEntity.getPrimaryKey());

em2.merge(unattachedEntity);
em3.merge(unattachedEntity);

- .

+3

Source: https://habr.com/ru/post/1709611/


All Articles