entityManager.persist(user) -> javax.persistence.EntityExistsException: User@b3089 is already persistent

余生长醉 提交于 2019-12-12 12:16:48

问题


// 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!