Reputation: 7551
Is there any specific configuration to put an entity from different persistence Unit into a current mapping?
For example:
@RooJpaActiveRecord(persistenceUnit = "persistenceUnit_central")
public class UserGroups{
//users come from `persistenceUnit_client`
//how to work this out?
//can mappedBy and targetEntity works the same way
//as they are in the same persistence unit?
@OneToMany
private List<User> users;
}
Thanks in advance.
Upvotes: 4
Views: 1627
Reputation: 7551
MaDa is correct. I made this answer to the question is just to hightlight the solution to this issue.
First we so far we cannot persist instance of entity A within entity B while A and B are from different persistence Unit. A safe way to make it work properly is to make instance of entity A becomes @Transient, then there will never get a change to make that instance tie to the database. However, it will be a little bit pain to manually set up the relationship between the entities(setter and getter), that becomes an open question.
Thanks again Mada.
Upvotes: 0
Reputation: 10762
I don't think you can't do it straightforward. Persistence units mean to be distinctly separate; they have different entity managers, so they may well be (and this is usually the reason) from different databases or schemas.
You still can define the same entity class to be present in several persistence units, in persistence.xml, but, as I said, it will be handled by each manager separately. This means you can't do this:
UserGroups ug = em1.find(UserGroups.class, ...); // entity manager 1
User u = em2.find(User.class, ...); // entity manager 2
// exception will be thrown on commit
// - from the point of view of em1, "u" is detached
ug.getUsers().add(u);
I'm not sure if calling em1.merge(u)
would solve the problem — I haven't yet come across such situation. But you surely can create a copy of User
and merge it into desired persistence context.
Upvotes: 2