简体   繁体   English

Google App Engine通过键获取对象

[英]Google App Engine Getting Object by the Key

I am stuck with geting a persisted object by its id. 我坚持通过其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. 这里cng是一个json字符串。 I set the key with a string: cNGID. 我用一个字符串设置密钥:cNGID。 I am trying to use the same id to get the object. 我正在尝试使用相同的ID来获取对象。

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

and end up getting the above error. 并最终得到上述错误。

您似乎尚未将其保存到数据存储中。

Datastore.put(cngEntity);

The constructor new Entity("CNG", cngKey) is defining the entity with kind and parent key . 构造函数new Entity("CNG", cngKey)正在使用kind和parent key定义实体。 Then when you try to retrieve it you do not provide parent key: KeyFactory.createKey("CNG", "T78") . 然后,当您尝试检索它时,不提供父键: 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) . 相反,您应该只使用new Entity(cngKey)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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