How can I created a detached copy of an attached object I've got from the entity manager?
I don't want to detach the original one, I want to create a copy of it while making sure that changes on the copied object won't effect the database.
I need to copy the attached object since it contains changes which were not yet written to the database. Afterwards I want to continue and working with the attached object (and do more changes).
Didn't test this so can't guarantee it'll work, but you can try something like this ( em.detach()
available since JPA 2.0)
MyEntity m1 = em.find(MyEntity.class, 1);
em.detach(m1);
MyEntity m2 = em.find(MyEntity.class, 1);
m2
should be attached, while m1
remains detached.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.