Search code examples
javajpaopenjpa

OpenJPA EntityExistsException "already exists in this context; another cannot be persisted", how to get an internal @idhex value?


I get an error object already exists when calling em.persist(serviceInstance) method. Object has several OneToMany(unidirectional) child object links, and one of the ServiceInstance -> ServiceInstanceProperty child object has this error.

I put SQL=TRACE logging but this error happens before sql formula is sent to a jdbc driver. Tried to sysout print prop.hashCode() but no match to a ServiceInstanceProperty@4f70aab2 value I can think of.

Is there a way to get more information about this object, hashCode()+key_values+values anything able to match to a property instance? Can I ask OpenJPA to give me @4f70aab2 value of the property instance?

Caused by: <openjpa-3.2.2-re5933d6 nonfatal store error> org.apache.openjpa.persistence.EntityExistsException:
An object of type "my.db.ServiceInstanceProperty" with oid "my.db.ServiceInstanceProperty-my.db.BeanProperty$PK@b0709ec6" 
already exists in this context; another cannot be persisted.
FailedObject: my.db.ServiceInstanceProperty@4f70aab2
    at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(BrokerImpl.java:5333)

https://github.com/apache/openjpa/blob/b3d1ef8671751d47617eaf4ac259b43eae45e53b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java#L5326

edit1: ok I was able to pinpoint an object by using a primary key BeanProperty$PK@b0709ec6 hashCode-to-hex value, primary key object is "never changed" hashCode value so easier to track object. Now need to study what went wrong in saving a jpa entities.

edit2: my copypaste mistake, updated two objects with the same PK key, secret was to print toHexString(obj.getPK().hashCode()) from the primary key as it's not anything set by an internal OpenJPA id tracking.


Solution

  • Secret was to print toHexString(obj.getPK().hashCode()) from the primary key as it's not anything set by an internal OpenJPA id tracking.

    oid "my.db.ServiceInstanceProperty-my.db.BeanProperty$PK@b0709ec6"