ibsenv ibsenv - 6 months ago 46
Java Question

How can you retrieve key of a datastore Entity

I have created datastore entity like below. I am trying to get the key of the entity by looping the query result. Also Is this the right way to get key of an entity?

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity e = new Entity("User");
e.setProperty("userName", user.getUserName());
e.setProperty("email", user.getEmail());

Query q = new Query("User")
PreparedQuery pq = ds.prepare(q);
Iterable<Entity> entityList = pq.asIterable();
for (Entity result : entityList) {
//how to get entity key from here


If you need a string representation of a key, you can do:

Key key = ds.put(e);
String keyString = KeyFactory.keyToString(key);