我正在使用带有 JDO(DataNucleus)的 GAE 1.7.0。当我持久化具有集合属性的类时,已移除的集合成员不会从数据存储中移除。我从分离的副本中删除集合成员。新成员被正确添加,现有成员没有被删除,导致我的收藏只会增长。
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Parent{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent(defaultFetchGroup="true", dependentElement="true")
private List<Child> children = new ArrayList<Child>(0);
}
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Child implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String id;
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk", value="true")
private String parentId;
....
}
...
parent = pm.detachCopy(resultFromQuery);
// parent looks correct in dubugger: all children are fetched (and detached)
....
parent.getChildren().clear();
parent.getChildren().addAll(newChildren);
for (Child child: parent.getChildren())
child.setParentId(KeyFactory.createKeyString("Parent", parent.getId()));
// parent looks good in the debugger: old children were removed and new ones added
...
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.currentTransaction().begin();
pm.makePersistent(parent);
pm.currentTransaction().commit();
} catch (Exception e) {
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
if (!pm.isClosed())
pm.close();
}
// problem in datastore: new children were created, old ones not removed
我注意到,如果我在事务中执行 query-remove-persist(不分离对象),那么问题就解决了。这是一个不错的临时解决方法,但我仍然想对分离的对象进行更新。