Appengine 实体的完全指定键

Appengine Entity's fully specified key

在实体的方法中 getKey() 指出:

Returns the Key that represents this Entity. If the entity has not yet been saved (e.g. via DatastoreService.put), this Key will not be fully specified and cannot be used for certain operations (like DatastoreService.get). Once the Entity has been saved, its Key will be updated to be fully specified.

完全指定是什么意思?

下面的测试中,DatastoreService#put(Entity)前后的key是一样的:

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity e = new Entity("Person", "John");

System.out.println(e.getKey()); // output: Person("John")
System.out.println(KeyFactory.keyToString(e.getKey())); // output: aglub19hcHBfaWRyEAsSBlBlcnNvbiIESm9obgw

Key k = ds.put(e);

System.out.println(k); // output: Person("John")
System.out.println(KeyFactory.keyToString(k)); // output: aglub19hcHBfaWRyEAsSBlBlcnNvbiIESm9obgw

我认为文档中存在歧义。而不是"will not be fully specified and cannot be used for certain operations",它应该说“可能未完全指定并且可能不用于某些操作”。

创建实体时,您提供了创建完全指定键所需的所有信息:实体种类 ("Person") 和标识符 ("John")。文档中的这段话适用于您仅指定实体种类的情况,在这种情况下,标识符将在保存实体时由数据存储自动生成。没有此标识符,密钥不能用于 get 操作。