detachCopy is working on JDO with ObjectDB?
pm.detachCopy is working?
I'm making a Spring + ObjectDB(JDO) program.
PersistenceManager#detachCopy returns a transient object despite of @PersistenceCapable:detachable is true.
here is a sample code.
I hava a simple test model(POJO)
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class TestModel {
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
@PrimaryKey
private Long id;
@Persistent
private String name;
// getter, setter
}
detachable is set to "true".
and dao is
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
}
}
I think I hava a detached state at (2). but is transient.
Version of ObjectDB is 2.4.0_05
application-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>
JDO requires enhancement of all the persistable classes. ObjectDB supports using persistable classes with no enhancement, as an extension to JDO, but not all the JDO features can be supported in that mode.
Particularly, when using instances of non enhanced persistence capable classes, transient and detached objects look the same (since the class is missing the extra fields that are added during enhancement to keep additional information).
Running your test with the TestModel class enhanced provides the expected result:
obj => persistent-new
detachedObj => detached-clean
链接地址: http://www.djcxy.com/p/60296.html