ibsenv ibsenv - 1 year ago 91
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

Answer Source

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

Key key = ds.put(e);
String keyString = KeyFactory.keyToString(key); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download