Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

java - org.apache.openjpa.persistence.EntityExistsException in jpa

I have a JTA transcation which manages transaction of 2 databases A and B.Now inside jta transcation i have a method that return a entity from db A.How i want to set some different values to some primary key (primary key is composit primary key) and then persist the entity as a new record.But am getting following exception:

                                 <openjpa-1.2.2-SNAPSHOT-r422266:778978M-OPENJPA-975 nonfatal store error> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "xyz.abc@616f991c".  If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting.
FailedObject: xyz.abc-
    at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2421)
    at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2280)
    at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1021)
    at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:645)
    at com.ibm.ws.jpa.management.JPAExEmInvocation.persist(JPAExEmInvocation.java:339)
    at com.ibm.ws.jpa.management.JPAEntityManager.persist(JPAEntityManager.java:133)
    at com.ibm.cloud.bss.db.data.controller.CostrateManager.createCostrate(CostrateManager.java:94)
    at com.ibm.cloud.bss.omt.catalog.impl.BundleManager.saveSubcomponentInECW(BundleManager.java:409)
    at com.ibm.cloud.bss.omt.catalog.impl.BundleManager.createBundle(BundleManager.java:274)
    at com.ibm.cloud.omt.OfferingManagementSOAPBindingImpl.createBundle(OfferingManagementSOAPBindingImpl.java:222)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:599)
    at org.apache.axis2.jaxws.server.dispatcher.JavaDispatcher.invokeTargetOperation(JavaDispatcher.java:81)
    at org.apache.axis2.jaxws.server.dispatcher.JavaBeanDispatcher.invoke(JavaBeanDispatcher.java:98)
    at org.apache.axis2.jaxws.server.EndpointController.invoke(EndpointController.java:109)
    at org.apache.axis2.jaxws.server.JAXWSMessageReceiver.receive(JAXWSMessageReceiver.java:159)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:188)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275)
    at com.ibm.ws.websvcs.transport.http.WASAxis2Servlet.doPost(WASAxis2Servlet.java:1389)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:831) 

I searched on net and i fold this post similar to myne: http://openjpa.208410.n2.nabble.com/persisting-an-entity-and-JPA-behaviour-with-referenced-entities-td210469.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From JPA 1.0 Spec:

A new entity instance becomes both managed and persistent by invoking the persist method on it or by cascading the persist operation. The semantics of the persist operation, applied to an entity X are as follows:

If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

If what you want is just change the primary key and the entity is a managed entity then just change its values and once the transaction is over those values will be persisted automatically. Another way is to change the values and call explicitly merge method.

But if what you want is to create a new object with the same properties of the retrieved object and keep the object retrieved intact you will have to first retrieve the object then create a new object and copy the properties from retrieved object to the new object and after that call the persist method on the new object. You can use BeanUtils.copyProperties method to copy the properties from a source object to a destination object or you can do it by your self.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...