简体   繁体   中英

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());
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
}

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

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

You can do:

for (Entity result : entityList) {
   Key userKey = result.getKey();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM