jpa中的org.apache.openjpa.persistence.EntityExistsException [英] org.apache.openjpa.persistence.EntityExistsException in jpa

查看:252
本文介绍了jpa中的org.apache.openjpa.persistence.EntityExistsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTA事务管理两个数据库A和B的事务.现在在jta事务中我有一个从db A返回一个实体的方法.我想为某个主键设置一些不同的值(主键是合成主键),然后将实体保留为新记录.但是出现以下异常:

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) 

我在网上搜索过,并且折叠了类似于myne的这篇文章: http://openjpa.208410.n2.nabble.com/persisting-an-entity-and-JPA-behaviour-with-referenced-entities-td210469.html

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

推荐答案

来自JPA 1.0规范:

From JPA 1.0 Spec:

通过在其上调用persist方法或级联persist操作,新的实体实例将变为托管实例和持久实例. 应用于实体X的persist操作的语义如下:

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:

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

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.

如果您要更改的只是主键,并且该实体是受管实体,则只需更改其值,一旦交易结束,这些值将自动保留.另一种方法是更改​​值并显式调用 merge 方法.

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.

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

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.

这篇关于jpa中的org.apache.openjpa.persistence.EntityExistsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆