Question

I am using JDO in GAE. I have two JDO classes having one to many relationship. parent class is

@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>();

}

The child classes is

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

I am just performing CURD operations on both entity. First i create the parent instance and then i create the child instance as

 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();
    }
}

My issue is created when i delete any child instance. As you see from the above function i link the child to the parents( means add child object into arrayList). So when child is deleted its reference in the parents is not deleted so at the detached time of parents object i got exception which is as

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)

The code of function retreiveDomainList in IDPJDOBroker is

 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;
}

Please tell me what to do? Is it not possible in JDO to delete the child? if it is possible then how to do it properly.

Was it helpful?

Solution

I have just seen this, but in case anyone arrives here, to delete a child object in a one to many relationship you must delete the reference from the parent, the child object will be deleted "transparently"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top