59276

org.apache.openjpa.persistence.EntityExistsException in jpa

Question:

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: <a href="http://openjpa.208410.n2.nabble.com/persisting-an-entity-and-JPA-behaviour-with-referenced-entities-td210469.html" rel="nofollow">http://openjpa.208410.n2.nabble.com/persisting-an-entity-and-JPA-behaviour-with-referenced-entities-td210469.html</a>

Answer1:

From JPA 1.0 Spec:

<blockquote>

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 <strong>EntityExistsException</strong> may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

</blockquote>

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 <strong>merge</strong> 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 <strong>persist</strong> method on the new object. You can use <strong>BeanUtils.copyProperties</strong> method to copy the properties from a source object to a destination object or you can do it by your self.

Recommend

  • DryIoc, Spring.Net's GetObjectsOfType equivalent?
  • (iphone) InAppSettings/InAppSettingsKit library questions
  • Custom view (xib) not visible on storyboard
  • Using UIImageView together with constraints
  • A weird problem - adding more UILabels to my UITableViewCell crashes the application when releasing
  • Can't get UITextField to autoshrink the font
  • UIButton in objective c [duplicate]
  • IOS - How to fit with to parent view use autolayout
  • Persistence.createEntityManagerFactory takes ages
  • Swift 3.0 Adding a Right Button to Navigation Bar
  • How to change the keyboard type for a text field in Xcode
  • How to get system properties __CreatedAt, __Version in javascript backend of Azure Mobile services?
  • Windows Themes ButtonChrome Border
  • Can't get UIScrollView to work
  • Java Errors for simple program stop compiling process, please help
  • UINavigationbar covers top of view
  • viewWillAppear is not called in my view controller when the app becomes active
  • UIButton event 'Touch Up Inside' not working. 'Touch Down' works fine
  • Is there a reliable way to extract years from a postgres interval?
  • two interfaces, multiple inheritance combine into one container?
  • Is it possible to create prototype cells in Interface Builder without story boards?
  • Joining tables without relation using JPA criteria
  • copy data from a mysql database to other mysql database with java
  • Sorting an array of pointers
  • Does Java EE 6 framework only for Web Application Or can I use it for Client Application as well
  • What is the use of the SHT_NULL section in ELF?
  • Cannot send user message with Spring Websocket
  • How to do JOIN ON query using Criteria API
  • Mocking Non-Standard Events in F# Foq
  • How do `pass` and `listen` work in WriterT?
  • UIPickerView without IB?
  • Configuring Ninject
  • How do I change the kernel/python version for iPython?
  • triggering user space with kernel
  • Sybase Error Implicit Conversion from datatype 'VARCHAR' to 'INT' not allowed
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Cross-Platform Protobuf Serialization
  • How to format a variable of double type
  • coudnt use logback because of log4j
  • JaxB to read class hierarchy