Reputation: 35346
What is the difference of GAE datastore Long id and String key in Key.
Using the Key
KeyFactory.createKey(kind, key);
KeyFactory.createKey(kind, id);
So in this case either can be used as Identifier. If I create a Key from KeyFactory.createKey(kind, key)
where key is a String I can see the id
field of the key is 0. In this case getId()
will be 0
? And in contrast, if what is set is KeyFactory.createKey(kind, someLongValue)
then getName()
will be a empty String
?
Upvotes: 2
Views: 1147
Reputation: 1102
An app engine Key can have EITHER a unique long ID, or a unique String name - they are mutually exclusive. If you set a long ID, the name will be null. Which you decide to use for any Entity kind really depends on your use case.
A benefit of using long IDs is that you can have app engine auto-generate them for you. That is, you can create your Entity without any value for its ID, and then when you save it it will be given a valid unique long ID by the datastore.
If you use a String name, you must create the unique name before the Entity is saved. This is useful in some cases if you have some property on the Entity that makes for a natural fit for the unique name (for example, and SKU for a part). Also, some people generate a random UUID for the String name - this ensures that all names are globally unique, not just unique among a particular Entity kind and ancestor path.
Upvotes: 2