1

我在 GAE 中使用 JDO。我有两个具有一对多关系的 JDO 类。父类是

@PersistenceCapable(detachable="true")
@FetchGroup(name="childerns", members={@Persistent(name="aliasName")})
public class IdentityProvider {

@PrimaryKey
@Persistent
private String url;
@Persistent
private String domainName;
@Persistent
@Element(dependent = "true")
private ArrayList<AliasDomain> aliasName = new ArrayList<AliasDomain>();

}

子班是

@PersistenceCapable(detachable = "true")
public class AliasDomain {
@Persistent
private String url;
@Persistent
private String aliasName;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
}

我只是在两个实体上执行 CURD 操作。首先我创建父实例,然后我将子实例创建为

 public void addAliasDomain(AliasDomain domain) {
    String url = domain.getUrl();
    PersistenceManager pm = PMFSingleton.get().getPersistenceManager();
    IdentityProvider idp = null;
    Transaction txn = null;
    try {
        txn = pm.currentTransaction();
        txn.begin();
        pm.getFetchPlan().addGroup("childerns");
        idp = pm.getObjectById(IdentityProvider.class, url);
        idp = pm.detachCopy(idp);
        idp.getAliasName().add(domain);
        pm.makePersistent(idp);
        txn.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if ( txn.isActive() ) {
            txn.rollback();
        }
        pm.close();
    }
}

当我删除任何子实例时,就会产生我的问题。正如您从上面的函数中看到的,我将孩子链接到父母(意味着将子对象添加到数组列表中)。所以当孩子被删除时,它在父母中的引用不会被删除,所以在父母对象分离的时候,我得到了异常,就像

Object of type "user.oauth.jdo.model.IdentityProvider" and identity "yahoo.com" was not     detached correctly. Please consult the log for any possible information.
org.datanucleus.exceptions.NucleusUserException: Object of type    "user.oauth.jdo.model.IdentityProvider" and identity "yahoo.com" was not detached    correctly. Please consult the log for any possible information.
at org.datanucleus.state.JDOStateManager.detachCopy(JDOStateManager.java:2942)
at org.datanucleus.ObjectManagerImpl.detachObjectCopy(ObjectManagerImpl.java:2591)
at   org.datanucleus.api.jdo.JDOPersistenceManager.jdoDetachCopy(JDOPersistenceManager.java:1145 )
    at   org.datanucleus.api.jdo.JDOPersistenceManager.detachCopy(JDOPersistenceManager.java:1174)
at user.oauth.data.broker.IDPJDOBroker.retrieveDomainList(IDPJDOBroker.java:49)

IDPJDOBroker 中函数 retreiveDomainList 的代码是

 public List retrieveDomainList() {
    PersistenceManager pm = PMFSingleton.get().getPersistenceManager();
    Query query = pm.newQuery(IdentityProvider.class);
    List<IdentityProvider> list = null;
    List<IdentityProvider> detachedList = null;
    IdentityProvider idp = null;
    try {
        pm.getFetchPlan().addGroup("childerns");
        list = (List<IdentityProvider>) query.execute();
        detachedList = new ArrayList<IdentityProvider>();
        for(IdentityProvider obj : list){
            idp = pm.detachCopy(obj);
            OAuthJDOBroker broker = new OAuthJDOBroker();
            int actUsers = 0;
            if ( idp.getHistory() != null && idp.getHistory().size() > 0) {
                actUsers = broker.calculateActiveUser(idp.getUserActiveDuration(),idp.getDomainName());
            }
            idp.setActiveUsers(actUsers);
            detachedList.add(idp);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        query.closeAll();
        pm.close();
    }
    return detachedList;
}

请告诉我该怎么办?JDO中不能删除孩子吗?如果可能的话,那么如何正确地做到这一点。

4

1 回答 1

1

我刚刚看到这个,但是如果有人到达这里,要删除一对多关系中的子对象,您必须删除父对象的引用,子对象将被“透明地”删除

于 2015-01-06T20:16:27.253 回答