简体   繁体   English

JDO(DataNucleus)集合成员未删除

[英]JDO(DataNucleus) collection members are not deleted

I'm using GAE 1.7.0 w/ JDO(DataNucleus). 我正在使用GAE 1.7.0 w / JDO(DataNucleus)。 When I persist a class with a collection attribute, removed collection members are not removed from the datastore. 当我保留具有collection属性的类时,不会将已删除的collection成员从数据存储中删除。 I remove the collection members from a detached copy. 我从分离副本中删除集合成员。 The new members are correctly added, the existing ones ar not removed, causing my collection to only grow. 正确添加了新成员,未删除现有成员,从而导致我的收藏仅增长。

@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

I noticed that if I do the query-remove-persist in a transaction (without detaching the object), then the problem is solved. 我注意到,如果我在事务中执行query-remove-persist(不分离对象),那么问题就解决了。 This is an ok temporal work-around, but I would still want to do the update on the detached object. 这是一个暂时的解决方法,但是我仍然想对分离的对象进行更新。

I am not an expert on JDO, but here goes.... 我不是JDO专家,但是...

Each Parent and its related Child entities are in the same entity group. 每个Parent及其相关的Child实体都在同一实体组中。 Therefore, you will need to do your datastore updates within a transaction, since a given entity group may not be updated with a frequency of more than around once per second. 因此,您将需要在事务内进行数据存储区更新,因为给定实体组的更新频率可能不会超过每秒一次。 Also, within a transaction the enhancement magic will work: hidden, added Java bytecode which will handle things like adding and removing children, and setting field values. 同样,在事务中,增强魔术将起作用:隐藏,添加的Java字节码,该字节码将处理诸如添加和删除子级以及设置字段值之类的事情。

If you do not want to do this, then one way is not to store a list of Child entities in the Parent ; 如果您不想执行此操作,则一种方法是不在Parent存储Child实体的列表。 store a list of Child IDs and/or primary keys instead. 而是存储Child ID和/或主键的列表。 This will result in each Child being in its own entity group. 这将导致每个Child都在其自己的实体组中。 But even then, you will be able to update each Parent (list) only once per second. 但是即使那样,您也只能每秒更新一次“ Parent (列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM