openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pawel Veselov <pawel.vese...@gmail.com>
Subject re-attaching detached entities
Date Tue, 24 Jan 2012 20:20:51 GMT
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.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message