views:

42

answers:

2
// 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  A: 

What about entityManager.merge(user)?

Also, see here.

James Earl Douglas
+2  A: 

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.

Pascal Thivent