Reputation: 2261
Heres my code
Entity post = new Entity("Post");
int ID = post.getKey().getId();
post.setProperty("ID", ID);
But the ID shows as 0 in my datastore.
What did I do wrong?
Upvotes: 0
Views: 460
Reputation: 19
You can use method allocateIds to generate unique ID.
Here is an example.
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
KeyRange keyRange = datastoreService.allocateIds("CartItem", 1L);
// you have ID here before save
Entity item = new Entity(keyRange.getStart());
datastoreService.put(item);
Upvotes: 0
Reputation: 12986
You won't have an id getId()
until the entity has been put to the datastore.
Why would you store the id from the Key as an property of an entity. It is completely redundant. Also the id of the key can't be changed re-inforcing redundancy.
Upvotes: 3