问题
// lookup existing user & set a currently null child entity (ContactInfo)
user.setContactInfo(contactInfo);
// update the user in the datastore with newly created child Entity
entityManager.persist(user);
Generates exception:
javax.persistence.EntityExistsException: User@b3089 is already persistent
Since the user already exists, is there some other method besides entityManager.persist that I should be using for an update instead of an insert?
回答1:
Your user instance is already loaded in the persistence context, just modifiy it and don't invoke any EntityManager method, the JPA provider will automatically update the database at flush or commit time.
回答2:
What about entityManager.merge(user)?
Also, see here.
来源:https://stackoverflow.com/questions/3527398/entitymanager-persistuser-javax-persistence-entityexistsexception-userb30