Google App Engine Getting Object by the Key

。_饼干妹妹 提交于 2019-12-13 05:37:15

问题


I am stuck with geting a persisted object by its id. I am getting an error:

com.google.appengine.api.datastore.EntityNotFoundException: No entity was found matching the key: CNG("T78")

I persist the object as below to the data store:

Key cngKey = KeyFactory.createKey("CNG", jsonCNG.cNGID);
Entity cngEntity = new Entity("CNG", cngKey);
cngEntity.setProperty("cng_name", jsonCNG.cNGName);
cngEntity.setProperty("cng_type", jsonCNG.cNGType);
cngEntity.setProperty("cng_content", cng);

Here cng is a json string. I set the key with a string: cNGID. I am trying to use the same id to get the object.

Key cngKey = KeyFactory.createKey("CNG", "T78")

and end up getting the above error.


回答1:


The constructor new Entity("CNG", cngKey) is defining the entity with kind and parent key. Then when you try to retrieve it you do not provide parent key: KeyFactory.createKey("CNG", "T78"). This does not work - you must either provide parent key in both places on not.

Note - defining entity parent is used when defining entity groups, which are in turn important when using transactions. You probably did not want that?

Instead you should just use new Entity(cngKey).




回答2:


You don't seem to have saved it to the datastore.

Datastore.put(cngEntity);


来源:https://stackoverflow.com/questions/19233213/google-app-engine-getting-object-by-the-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!