2013-10-07 46 views
0

我被堅持通過其編號創建持久對象。我得到一個錯誤:Google App Engine通過密鑰獲取對象

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

我堅持的對象,如下到數據存儲:

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來獲取對象。

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

並最終得到上述錯誤。

回答

1

構造new Entity("CNG", cngKey)的定義與種類和父鍵的實體。然後當您嘗試檢索它時,您不提供父鍵:KeyFactory.createKey("CNG", "T78")。這不起作用 - 你必須在兩個地方都提供父鍵。

注 - 在定義實體組時使用定義實體父項,這在使用事務處理時非常重要。你可能不想要那個?您可以使用new Entity(cngKey)

1

您似乎沒有將其保存到數據存儲區。

Datastore.put(cngEntity); 
+0

其實,我把它保存到數據源。抱歉代碼丟失。 –

相關問題