title: Prevent OpenJPA from flushing until commit time
body: <p>I'm using OpenJPA 1.2.3, the default version that comes with WebSphere 7.0. </p>
  
  <p>What I'm trying to do is to prevent OpenJPA from flushing until commit time. I thought this would easily be accomplished by setting the Entity Manager flush mode type to FlushModeType.COMMIT. Below is the code that get the Entity Manager and set the flush mode. </p>
  
  <pre><code>public EntityManager getEntityManager(String name) {
      EntityManager em = null;
      try {
          em = (EntityManager) this.context.lookup("java:comp/env/" + name);
          em.setFlushMode(FlushModeType.COMMIT);
      } catch (NamingException e) {
          logger.error("Unable to get EntityManager for " + name, e);
      }
      return em;
  }
  </code></pre>
  
  <p>I've also added the</p>
  
  <pre><code>&lt;property name="openjpa.FlushBeforeQueries" value="false"/&gt;
  </code></pre>
  
  <p>property to my persistence.xml, but OpenJPA still flushes before executing queries to the database. </p>
  
  <p>Below is an example error message, I get during a flush before executing a query. I know why I get that error, and it can easily be fixed, but due a limiting in the existing custom logics (and it can't be modified), I need OpenJPA to defer flushing until commit time. </p>
  
  <pre><code>2015-03-10 21:43:45.622 class org.apache.openjpa.persistence.InvalidStateException:
  Encountered unmanaged object in persistent field
  "com.xxx.yyy.common.model.domain.XxxWorkorder.xxxBundle" during flush.
  However, this field does not allow cascade persist. Set the cascade attribute for this field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or "all" (JPA orm.xml), or enable cascade-persist globally, or
  manually persist the related field value prior to flushing. You cannot flush unmanaged objects or graphs that have persistent associations to unmanaged objects.
   at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(SingleFieldManager.java:754)
   at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:594)
   at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:562)
   at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:478)
   at org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2829)
   at org.apache.openjpa.kernel.PDirtyState.beforeFlush(PDirtyState.java:37)
   at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:960)
   at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1967)
   at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1927)
   at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1698)
   at org.apache.openjpa.kernel.QueryImpl.isInMemory(QueryImpl.java:956)
   at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:796)
   at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:775)
   at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:771)
   at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:517)
   at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:255)
   at org.apache.openjpa.persistence.QueryImpl.getSingleResult(QueryImpl.java:317)
  </code></pre>
  
  <p>My persistence.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;persistence 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_1_0.xsd"
   version="1.0"&gt;
   &lt;persistence-unit name="MaxPU" transaction-type="JTA"&gt;
     &lt;provider&gt;org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider&gt;
     &lt;jta-data-source&gt;jdbc/MaximoDatabase&lt;/jta-data-source&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundleStatus&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.DomainObject&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxWorkorder&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxWr&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxAcknowledge&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundleAppl&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundleEvent&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundleAsset&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundleMemo&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundlePort&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxChildDomain&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxJobPlan&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxOrderErCd&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxOrderErLog&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxOrderRule&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxOrderRuleFld&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxStatusRule&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxVentyxMsgCntrl&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxVentyxMsgCntrlData&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundHist&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxDataEditRule&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxExportMstr&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxGoprtyRule&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxJobValRule&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxPrintLoc&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxWoHist&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxWorkgroup&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxCoprtyRule&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.XxxBundle&lt;/class&gt;
     &lt;class&gt;com.xxx.yyy.common.model.domain.Labor&lt;/class&gt;
     &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
     &lt;properties&gt;
       &lt;!-- OpenJPA specific properties --&gt;
       &lt;property name="openjpa.TransactionMode" value="managed"/&gt;
       &lt;property name="openjpa.ConnectionFactoryMode" value="managed"/&gt;
       &lt;property name="openjpa.jdbc.DBDictionary" value="oracle"/&gt;
       &lt;property name="openjpa.FlushBeforeQueries" value="false"/&gt;
     &lt;/properties&gt;
   &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Any suggestions would be appreciated! </p>
  
  <p>Thanks</p>