user4413257
user4413257

Reputation:

Appengine Entity's fully specified key

In Entity's method getKey() states:

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.

What does the fully specified mean?

In following test, keys are the same before and after DatastoreService#put(Entity):

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

Upvotes: 0

Views: 44

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

I think there is an ambiguity in the documentation. Instead of "will not be fully specified and cannot be used for certain operations" it should say "may not be fully specified and may not be used for certain operations".

When you created an entity, you provided all the information necessary to create a fully specified key: entity kind ("Person") and an identifier ("John"). This passage from the documentation applies to situations when you only specify an entity kind, in which case the identifier will be auto-generated by the Datastore when the entity is saved. Without this identifier the key cannot be used for a get operation.

Upvotes: 1

Related Questions