Note that there are some explanatory texts on larger screens.

plurals
  1. POCan one entity be persisted to multiple persistence units using OpenJPA?
    primarykey
    data
    text
    <p>I'm trying to save an entity to two separate persistence units one after the other. I can successfully save the entity to the first unit, I then detach it from that unit, reset the <code>@Id</code> value and persist to the second, but it appears that the object still has an associated id that is possibly not set-able? I think it's called the oid? The error:</p> <pre><code>Caused by: &lt;openjpa-2.2.0-r422266:1244990 nonfatal store error&gt; 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. </code></pre> <p>I know I can create a brand new object and copy the values I want across, but I want to do this generically without knowing too much about the object itself.</p> <p>My code looks like this:</p> <pre><code> @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 </code></pre> <p>And here is the persistence.xml</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;persistence-unit name="puOpenJPA_MSSQL" transaction-type="JTA"&gt; &lt;provider&gt; org.apache.openjpa.persistence.PersistenceProviderImpl &lt;/provider&gt; &lt;jta-data-source&gt; java:jboss/datasources/mySqlSandbox &lt;/jta-data-source&gt; &lt;class&gt; za.co.core.ejb.entities.AuditableEntity &lt;/class&gt; &lt;class&gt;za.co.core.ejb.entities.Address&lt;/class&gt; &lt;properties&gt; &lt;property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" /&gt; &lt;property name="jboss.as.jpa.providerModule" value="org.apache.openjpa" /&gt; &lt;property name="openjpa.DynamicEnhancementAgent" value="false"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;persistence-unit name="puOpenJPA_MSSQLaudit" transaction-type="JTA"&gt; &lt;provider&gt; org.apache.openjpa.persistence.PersistenceProviderImpl &lt;/provider&gt; &lt;jta-data-source&gt; java:jboss/datasources/mySqlSandboxAudit &lt;/jta-data-source&gt; &lt;class&gt;za.co.core.ejb.entities.AuditableEntity&lt;/class&gt; &lt;class&gt;za.co.core.ejb.entities.Address&lt;/class&gt; &lt;properties&gt; &lt;property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" /&gt; &lt;property name="jboss.as.jpa.providerModule" value="org.apache.openjpa" /&gt; &lt;property name="openjpa.DynamicEnhancementAgent" value="false" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>Thanks,</p> <p>Sean</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload