Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] cloning a JPA entity by using detach

You will probably need to persist your detached objects in an EntityManager derived from a new EntityManagerFactory to have this work.

janne postilista wrote:
Hi,

 I need to create clones of some of my JPA entities, and modify some
of their fields. My plan was to:

1. load some JPA entities: o = em.find(Person.class, id);
2. detach them with em.detach(o)
3. update a few fields and set @Id to null to make JPA create new rows
instead of updating the old ones
4. em.merge(o)

But this fails since I am not allowed to update the @Id field:

Caused by: Exception [EclipseLink-7251] (Eclipse Persistence Services
- 2.0.1.v20100213-r6600):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [com.bzz.Person] is
mapped to a primary key column in the database. Updates are
not allowed.
        at org.eclipse.persistence.exceptions.ValidationException.primaryKeyUpdateDisallowed(ValidationException.java:2400)

QUESTION: is there any way around this? Is there some JPA -related
strategy that I could use to conveniently clone JPA entities?
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top