Reputation: 619
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());
ds.put(e);
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
}
Upvotes: 1
Views: 1761
Reputation: 41089
If you need a string representation of a key, you can do:
Key key = ds.put(e);
String keyString = KeyFactory.keyToString(key);
Upvotes: 2
Reputation: 1994
You can do:
for (Entity result : entityList) {
Key userKey = result.getKey();
}
Upvotes: 1