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>