2012-06-12 29 views
1

pm.detachCopy正在工作?detachCopy正在使用JDO與ObjectDB?

我在做一個Spring + ObjectDB(JDO)程序。

PersistenceManager#detachCopy儘管@PersistenceCapable返回瞬態對象:detachable爲true。

這裏是一個示例代碼。

我哈瓦一個簡單的測試模型(POJO)

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true") 
public class TestModel { 
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) 
@PrimaryKey 
private Long id; 
@Persistent 
private String name; 
// getter, setter 
} 

可拆卸設置爲 「真」。

和DAO是

public class TestModelDaoImpl { 
    private PersistenceManagerFactory persistenceManagerFactory; 
    public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) { 
     this.persistenceManagerFactory = pmf; 
    } 

    public TestModel makePersistent(TestModel obj){ 
     PersistenceManager pm = persistenceManagerFactory.getPersistenceManager(); 
     Transaction tx = pm.currentTransaction(); 
     tx.begin(); 
     pm.makePersistent(obj); 
     System.out.println(" obj => " + JDOHelper.getObjectState(obj)); // => (1) persistent-new 
     TestModel detachedObj = pm.detachCopy(obj); 
     System.out.println(" detachedObj => " + JDOHelper.getObjectState(detachedObj)); // => (2) transient .. 
     tx.commit(); 
     return detachedObj; 
     // try catch is omitted 
    } 
} 

我認爲在哈瓦分離狀態(2)。但是是暫時的。

ObjectDB的版本爲2.4.0_05

應用程序的context.xml

 
    <bean id="pmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean"> 
     <property name="jdoProperties"> 
      <props> 
       <prop key="javax.jdo.PersistenceManagerFactoryClass">com.objectdb.jdo.PMF</prop> 
       <prop key="javax.jdo.option.ConnectionURL">$objectdb/db/testdb.odb</prop> 
       <prop key="javax.jdo.option.ConnectionUserName">admin</prop> 
       <prop key="javax.jdo.option.ConnectionPassword">admin</prop> 
      </props> 
     </property> 
    </bean> 
    <bean id="jdoTransactionManager" class="org.springframework.orm.jdo.JdoTransactionManager"> 
     <property name="persistenceManagerFactory"> 
      <ref local="pmfProxy"/> 
     </property> 
    </bean> 
    <bean id="pmfProxy" class="org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy"> 
     <property name="targetPersistenceManagerFactory" ref="pmf"/> 
     <property name="allowCreate" value="true"/> 
    </bean> 

回答

0

JDO需要增強所有的持久化類。 ObjectDB支持使用不增強的持久化類作爲JDO的擴展,但並不是所有的JDO特性都可以在該模式下得到支持。

特別是,當使用非增強型持久性能力類的實例時,瞬態和分離對象看起來相同(因爲該類缺少在增強期間添加的額外字段以保留附加信息)。

運行具有增強的TestModel類測試提供了預期的結果:

的obj =>新持久

detachedObj =>拆下清洗