I'm using v2.2.3 of OpenJPA with DB2 v10.5 and I've run across a problem that occurs when an entity contains a field annotated with @Generated but yet the field is not the primary key for the entity.
Here's an approximation of my entity: @Entity @Table(name="A") public class A { private int id; private int realId; private String someField; public A() { } public A(int realId) { this.realId = realId; } @Basic @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(unique=true, insertable=false, updatable=false) public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement @Id @Column(name="REAL_ID", insertable=false, updatable=false) public int getRealId() { return realId; } public void setRealId(int realId) { this.realId = realId; } @Basic @XmlElement public String getSomeField() { return someField; } public void setSomeField(String someField) { this.someField = someField; } } Note that the "realId" field is not @Generated because it is actually a foreign key to another entity, hence it gets its value from that relationship. When I try to insert an entity hierarchy containing multiple instances of entity A (within the same transaction), I get an error like the following: <openjpa-2.2.3-SNAPSHOT-r422266:1620420 fatal store error> org.apache.openjpa.persistence.RollbackException: This operation failed for some instances. See the nested exceptions array for details. at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:594) ... Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1620420 fatal general error> org.apache.openjpa.persistence.PersistenceException: This operation failed for some instances. See the nested exceptions array for details. at org.apache.openjpa.kernel.BrokerImpl.throwNestedExceptions(BrokerImpl.java:2548) at org.apache.openjpa.kernel.BrokerImpl.endTransaction(BrokerImpl.java:2465) at org.apache.openjpa.kernel.BrokerImpl.afterCompletion(BrokerImpl.java:2025) at org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:94) at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1529) at org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:933) at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:570) ... 4 more Caused by: <openjpa-2.2.3-SNAPSHOT-r422266:1620420 fatal user error> org.apache.openjpa.persistence.ArgumentException: Attempt to assign id "0" to new instance "my.package.A-0" failed; there is already an object in the L1 cache with this id. You must delete this object (in a previous transaction or the current one) before reusing its id. This error can also occur when a horizontally or vertically mapped classes uses auto-increment application identity and does not use a hierarchy of application identity classes. FailedObject: my.package.A-0 at org.apache.openjpa.kernel.ManagedCache.commitNew(ManagedCache.java:236) at org.apache.openjpa.kernel.BrokerImpl.setStateManager(BrokerImpl.java:4145) at org.apache.openjpa.kernel.StateManagerImpl.commit(StateManagerImpl.java:1164) at org.apache.openjpa.kernel.BrokerImpl.endTransaction(BrokerImpl.java:2433) ... 9 more The odd thing is that all of the instances of entity A are actually committed to the database and the values of the ID column are properly generated, etc. Notice that the exception doesn't occur during the actuall call to EntityManager.merge(), but instead it occurs during the call to EntityManager.commit(). This seems like OpenJPA has perhaps failed to copy the generated values for the ID column back into their respectiive entity instances, and then incorrectly detects that multiple entity instances are attempting to use the same value for a column with a unique contraint on it. Any ideas on how to work around this? Thanks! Phil Adams -- View this message in context: http://openjpa.208410.n2.nabble.com/Problem-when-using-Generated-with-non-primary-key-column-tp7587738.html Sent from the OpenJPA Users mailing list archive at Nabble.com.