jpa 中的 org.apache.openjpa.persistence.EntityExistsException
我有一个 JTA 事务,它管理 2 个数据库 A 和 B 的事务。现在在 jta 事务中,我有一个从数据库 A 返回实体的方法。我想如何为某个主键设置一些不同的值(主键是复合主键) ),然后将实体保留为新记录。但是出现以下异常:
<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)
我在网上搜索并折叠了类似于我的帖子: http:// /openjpa.208410.n2.nabble.com/persisting-an-entity-and-JPA-behaviour-with-referenced-entities-td210469.html
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 JPA 1.0 规范开始:
如果您想要的只是更改主键并且该实体是托管实体,那么只需更改其值,一旦事务结束,这些值将自动保留。另一种方法是更改值并显式调用 merge 方法。
但是,如果您想要创建一个与检索到的对象具有相同属性的新对象并保持检索到的对象完整,则必须首先检索该对象,然后创建一个新对象并将属性从检索到的对象复制到新对象,然后之后在新对象上调用 persist 方法。您可以使用 BeanUtils.copyProperties 方法将属性从源对象复制到目标对象,也可以自己完成。
From JPA 1.0 Spec:
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.