You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Pawel Veselov <pa...@gmail.com> on 2012/01/24 21:20:51 UTC

re-attaching detached entities

Hi.

Also not sure if what I'm doing is that despicable.

There is an entity, with auto-generated PK. I want to clone this entity
into a new one. The entity is also abstract, so the logic behind cloning is
somewhat complicated, if done by manually copying each field. So I'm trying
to cheat a little:

MyEntity s = em.find(MyEntity.class, id);
em.detach(s);
s.setId(null);
em.merge(s);

I get:

<openjpa-2.1.1-r422266:1148538 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException: Attempt to
change a primary key field of an instance that already has a final
object id.  Only new, unflushed instances whose id you have not
retrieved can have their primary keys changed.
FailedObject: com.gsm.perftest.e.EScenario_Tag-758
	org.apache.openjpa.kernel.StateManagerImpl.assertNoPrimaryKeyChange(StateManagerImpl.java:2101)
	org.apache.openjpa.kernel.StateManagerImpl.settingObjectField(StateManagerImpl.java:2036)
	org.apache.openjpa.kernel.DetachedStateManager.attach(DetachedStateManager.java:288)
	org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:251)
	org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:104)
	org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3447)
	org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1214)
	org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:877)


I totally agree why changing PKs is a very bad idea, but my entity was
explicitly detached, effectively turning it into a bean, re-attaching it
should be treated as attaching any other object, like a newly created one.

OpenJPA 2.1.1

Thank you,
  Pawel.

Re: re-attaching detached entities

Posted by Pinaki Poddar <pp...@apache.org>.
try merging to a different em.

-----
Pinaki Poddar
Chair, Apache OpenJPA Project
--
View this message in context: http://openjpa.208410.n2.nabble.com/re-attaching-detached-entities-tp7221649p7223256.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.