当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: