数据存储区中的密钥不正确

时间:2012-07-11 06:06:59

标签: google-app-engine google-cloud-datastore

写入数据存储区

Key dataKey = KeyFactory.createKey("Datastore", "123");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity data = new Entity("Datastore", dataKey);
data.setProperty("date", date);

try{
   datastore.get(dataKey);
   datastore.delete(dataKey);
}catch(EntityNotFoundException ex){
   log.serve("Error : "+ ex.getMessage());
}

datastore.put(data);

从数据存储中读取

Key dataKey = KeyFactory.createKey("Datastore", "123");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity = datastore.get(dataKey);

我有02个问题:
1。我使用datastore.get(dataKey)检查实体是否存在,因为我想避免使用相同密钥的重复实体,但我不认为我使用的方式是好的。有没有其他方法可以做得更好?
2。我无法获取数据。它说“找不到匹配密钥的实体:数据存储区(”123“)” 我一次又一次地阅读谷歌数据存储文档,但我仍然无法找出这个错误:(。
有人可以帮帮我吗?
谢谢。

1 个答案:

答案 0 :(得分:1)

替换:

Entity data = new Entity("Datastore", dataKey)

使用:

Entity data = new Entity(dataKey)

此外,您不必担心重复项,如果您提交现有条目(其密钥已在数据存储区中),它将被覆盖。数据存储区中不能有两个相同的密钥。