jpa 中的 org.apache.openjpa.persistence.EntityExistsException

发布于 2024-10-10 07:51:43 字数 2822 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2024-10-17 07:51:43

从 JPA 1.0 规范开始:

通过调用新的实体实例的 persist 方法或级联 persist 操作,它既可以被托管,也可以被持久化。
应用于实体 X 的持久操作的语义如下:

如果X是一个分离的对象,持久化时可能会抛出EntityExistsException
调用操作,或者 EntityExistsException 或另一个 PersistenceException
可能会在刷新或提交时抛出。

如果您想要的只是更改主键并且该实体是托管实体,那么只需更改其值,一旦事务结束,这些值将自动保留。另一种方法是更改​​值并显式调用 merge 方法。

但是,如果您想要创建一个与检索到的对象具有相同属性的新对象并保持检索到的对象完整,则必须首先检索该对象,然后创建一个新对象并将属性从检索到的对象复制到新对象,然后之后在新对象上调用 persist 方法。您可以使用 BeanUtils.copyProperties 方法将属性从源对象复制到目标对象,也可以自己完成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文