3

我正在尝试将一个实体一个接一个地保存到两个单独的持久性单元中。我可以成功地将实体保存到第一个单元,然后将其与该单元分离,重置@Id值并保留到第二个单元,但似乎该对象仍然具有可能无法设置的关联 ID?我认为它被称为oid?错误:

Caused by: <openjpa-2.2.0-r422266:1244990 nonfatal store error> 
org.apache.openjpa.persistence.EntityNotFoundException: The instance 
of type "class za.co.core.ejb.entities.Address" with oid "4" no longer 
exists in the data store.  This may mean that you deleted the instance 
in a separate transaction, but this context still has a cached version.

我知道我可以创建一个全新的对象并复制我想要的值,但我想在不了解对象本身太多的情况下一般地执行此操作。

我的代码如下所示:

   @PersistenceContext(unitName = "puOpenJPA_MSSQL", 
      type = PersistenceContextType.TRANSACTION)
   private EntityManager entityManager;

   @PersistenceContext(unitName = "puOpenJPA_MSSQLaudit", 
      type = PersistenceContextType.TRANSACTION)
   private EntityManager auditManager;

   ...

   entityManager.persist(entity);
   entityManager.detach(entity);

   entity.setId(null); //this sets the @id property of the entity to null
   auditManager.persist(entity); //exception thrown

这是persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0"
       xmlns="http://java.sun.com/xml/ns/persistence"         
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
       <persistence-unit name="puOpenJPA_MSSQL" transaction-type="JTA">
           <provider>
              org.apache.openjpa.persistence.PersistenceProviderImpl
           </provider>
           <jta-data-source>
              java:jboss/datasources/mySqlSandbox
           </jta-data-source>
           <class>
              za.co.core.ejb.entities.AuditableEntity
           </class>
           <class>za.co.core.ejb.entities.Address</class>
           <properties>
               <property name="openjpa.jdbc.SynchronizeMappings" 
                 value="buildSchema(ForeignKeys=true)" />
               <property name="jboss.as.jpa.providerModule" 
                 value="org.apache.openjpa" />
               <property name="openjpa.DynamicEnhancementAgent" 
                 value="false"/>
       </properties>
   </persistence-unit>

   <persistence-unit name="puOpenJPA_MSSQLaudit" transaction-type="JTA">
       <provider>
         org.apache.openjpa.persistence.PersistenceProviderImpl
       </provider>
       <jta-data-source>
          java:jboss/datasources/mySqlSandboxAudit
       </jta-data-source>
       <class>za.co.core.ejb.entities.AuditableEntity</class>
       <class>za.co.core.ejb.entities.Address</class>

       <properties>
           <property name="openjpa.jdbc.SynchronizeMappings" 
             value="buildSchema(ForeignKeys=true)" />
           <property name="jboss.as.jpa.providerModule" 
             value="org.apache.openjpa" />
           <property name="openjpa.DynamicEnhancementAgent" 
             value="false" />
       </properties>
   </persistence-unit>

</persistence>

谢谢,

肖恩

4

1 回答 1

1

理论上是的,因为实体是“普通的旧 Java 对象”,但在实践中,为了使所有这些神奇工作,持久性提供程序代理它的一部分,如集合成员。一旦你坚持它,它就不再是“你的”实体——它是提供商簿记的一部分。

如果您想多次保存同一个实体,请多次克隆它并保存每个单独的副本。

于 2012-11-27T09:05:38.153 回答