Will JPA try to cascade persist on an entity that is already persistent (and non-detached)?
To make things clear, here's my situation: I want to persist a new User:
public void addUser(){
//User is an entity that is related to many Groups
//The relationship on User side is marked with cascade persist
User user = new User();
user.setName("foo");
user.setGroups(new ArrayList<Groups>());
//selectedGroups is an List of Groups previously created with persistent Groups
for(Group g : this.selectedGroups){
user.getGroups().add(this.groupDao.find(g.getId()));
}
//At this point, User.getGroups() is a list of Managed Entities
//What happens on the following line?
userDao.persist(user);
}
So, what happens? Will JPA try to persist (again) every single group in user.getGroups()? Or will it detect that those groups already exist and just update new relations?
In case of 'Yes, it will persist again', how should I annotate this relation to make that code work properly?