可以使用OpenJPA将一个实体持久化到多个持久性单元吗?

时间:2012-11-27 07:38:28

标签: java persistence openjpa

我正在尝试将一个实体一个接一个地保存到两个独立的持久性单元中。我可以成功地将实体保存到第一个单元,然后我将其从该单元中分离,重置@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>

谢谢,

肖恩

1 个答案:

答案 0 :(得分:1)

理论上是的,因为实体是“普通的旧Java对象”,但实际上,为了完成所有神奇工作,持久性提供程序代理它的一部分,如集合成员。一旦你坚持下去,它就不再是“你的”实体 - 它是提供者簿记的一部分。

如果您想多次保留同一个实体,请多次克隆并保留每个副本。