3

エンティティを 2 つの個別の永続化ユニットに次々に保存しようとしています。エンティティを最初のユニットに正常に保存できます。次に、エンティティをそのユニットから切り離し、@Id値をリセットして 2 番目のユニットに保持しますが、オブジェクトには、設定できない可能性のある ID が関連付けられているように見えますか? オイと呼ばれているのでしょうか。エラー:

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 に答える