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

Hello

There may be a better way to to this, but I use this approach:

http://stackoverflow.com/questions/1106632/deep-copy-in-jpa

I make a deep copy of the object and I get a valid Id from a temp, not persisted object 

clonedObject.setId(new MyEntityClass().getId());

em.persist(clonedObject);

Hope this helps

-------------------------------------
Charles Edward Bedón Cortázar
ITIL Foundation Certified
Open Source Network Inventory for the masses!  http://kuwaiba.sourceforge.net
Linux Registered User #386666


---- Am Wed, 22 Sep 2010 10:00:03 -0500 janne postilista <jannepostilistat@xxxxxxxxx> schrieb ----

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