问题
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