防止OpenJPA刷新直到提交时间

时间:2015-03-11 21:25:58

标签: java java-ee jpa websphere openjpa

我正在使用OpenJPA 1.2.3,这是WebSphere 7.0附带的默认版本。

我要做的是阻止OpenJPA在提交时间之前进行刷新。我认为这可以通过将实体管理器刷新模式类型设置为FlushModeType.COMMIT来轻松完成。下面是获取实体管理器并设置刷新模式的代码。

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;
}

我还添加了

<property name="openjpa.FlushBeforeQueries" value="false"/>

属性到我的persistence.xml,但OpenJPA在执行对数据库的查询之前仍然会刷新。

下面是一个示例错误消息,我在执行查询之前在刷新期间获得。我知道为什么我会收到该错误,并且可以轻松修复,但由于现有自定义逻辑的限制(并且无法修改),我需要OpenJPA将刷新推迟到提交时间。

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)

我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
    <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">
 <persistence-unit name="MaxPU" transaction-type="JTA">
   <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
   <jta-data-source>jdbc/MaximoDatabase</jta-data-source>
   <class>com.xxx.yyy.common.model.domain.XxxBundleStatus</class>
   <class>com.xxx.yyy.common.model.domain.DomainObject</class>
   <class>com.xxx.yyy.common.model.domain.XxxWorkorder</class>
   <class>com.xxx.yyy.common.model.domain.XxxWr</class>
   <class>com.xxx.yyy.common.model.domain.XxxAcknowledge</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundleAppl</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundleEvent</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundleAsset</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundleMemo</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundlePort</class>
   <class>com.xxx.yyy.common.model.domain.XxxChildDomain</class>
   <class>com.xxx.yyy.common.model.domain.XxxJobPlan</class>
   <class>com.xxx.yyy.common.model.domain.XxxOrderErCd</class>
   <class>com.xxx.yyy.common.model.domain.XxxOrderErLog</class>
   <class>com.xxx.yyy.common.model.domain.XxxOrderRule</class>
   <class>com.xxx.yyy.common.model.domain.XxxOrderRuleFld</class>
   <class>com.xxx.yyy.common.model.domain.XxxStatusRule</class>
   <class>com.xxx.yyy.common.model.domain.XxxVentyxMsgCntrl</class>
   <class>com.xxx.yyy.common.model.domain.XxxVentyxMsgCntrlData</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundHist</class>
   <class>com.xxx.yyy.common.model.domain.XxxDataEditRule</class>
   <class>com.xxx.yyy.common.model.domain.XxxExportMstr</class>
   <class>com.xxx.yyy.common.model.domain.XxxGoprtyRule</class>
   <class>com.xxx.yyy.common.model.domain.XxxJobValRule</class>
   <class>com.xxx.yyy.common.model.domain.XxxPrintLoc</class>
   <class>com.xxx.yyy.common.model.domain.XxxWoHist</class>
   <class>com.xxx.yyy.common.model.domain.XxxWorkgroup</class>
   <class>com.xxx.yyy.common.model.domain.XxxCoprtyRule</class>
   <class>com.xxx.yyy.common.model.domain.XxxBundle</class>
   <class>com.xxx.yyy.common.model.domain.Labor</class>
   <exclude-unlisted-classes>true</exclude-unlisted-classes>
   <properties>
     <!-- OpenJPA specific properties -->
     <property name="openjpa.TransactionMode" value="managed"/>
     <property name="openjpa.ConnectionFactoryMode" value="managed"/>
     <property name="openjpa.jdbc.DBDictionary" value="oracle"/>
     <property name="openjpa.FlushBeforeQueries" value="false"/>
   </properties>
 </persistence-unit>
</persistence>

任何建议将不胜感激!

由于

0 个答案:

没有答案