简体   繁体   中英

JDOObjectNotFoundException when trying to get a child object in GAE

I have designed a data model where there are parents and children objects (one to many). First I did all the job manually and stored ID's of parents in children objects to keep the relation. Then I decided to use relationship with documentations of app engine. Now I have a parent with ID 21 and a child with ID 1 (I suppose ID is 1 because this child is the only and the first child of this parent). Now I am trying to get the key as: child.getKey()

And with the same String I am trying to get the object with:

Child child = pm.getObjectById(Child.class, key);

Somehow I get this error: WARNING: /admin.jsp javax.jdo.JDOObjectNotFoundException: Could not retrieve entity of kind Child with key Child("Parent(21)/Child(1)")

I know that this child exists in this parent. Can maybe someone help me? I have researched about this and nothing showed up...

I have found the solution after hours of trying every possiblity. There are 2 ways how to solve this problem. First of all if you want to get a child object with the key, be sure that key is not a String. It should be a Key (com.google.appengine.api.datastore.Key). You can get this key in 2 different ways:

Key key = new KeyFactory
    .Builder(Parent.class.getSimpleName(), ParentID)
    .addChild(Child.class.getSimpleName(), ChildID).getKey();

or

Key key = KeyFactory.stringToKey(keyString); //you can obtain keyString with KeyFactory.keyToString(ChildObject.getKey());

Then you can easily use:

Child child = pm.getObjectById(Child.class, key);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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