我有一个JTA事务管理两个数据库A和B的事务。现在在jta事务中我有一个从db 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)


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

最佳答案

从JPA 1.0规范开始:


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

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


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

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

10-08 12:55