Google App Engine通过密钥获取对象 [英] Google App Engine Getting Object by the Key
问题描述
我坚持使用id来设置持久化对象。我收到错误消息:
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);
这里cng是一个json字符串。我用字符串设置密钥:cNGID。我试图使用相同的ID来获取对象。
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")
并最终得到上述错误。
推荐答案
构造函数 新实体(CNG,cngKey)
使用kind和父键定义实体。然后,当您尝试检索它时,您不提供父键: KeyFactory.createKey(CNG,T78)
。这不起作用 - 你必须在两个地方都不提供父键。
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?
相反你应该只使用新实体(cngKey)
。
这篇关于Google App Engine通过密钥获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!