简体   繁体   中英

How to delete an entity from a datastore, using java datastore.delete(key)

I've been trying to delete an entity for 2 nights now, without success. And I'm sure my problem is simple for many of you.

I successfully put() an entity using User.getNickName() as key:

 Key aKey = KeyFactory.createKey("Gamer2", user.getNickname()); 
 Entity aGamer = new Entity("Gamer2", aKey);
 aGamer.setProperty("nickName", user.getNickname());
 DatastoreService aDS = DatastoreServiceFactory.getDatastoreService();
 aDS.put(aGamer);

But I can't figure out how to do the delete.

Any help is appreciated. Please also let me know if my question has incomplete info.

Cheers!

This is how you delete it:

DatastoreService aDS = DatastoreServiceFactory.getDatastoreService();

Key aKey = KeyFactory.createKey("Gamer2", user.getNickname());
aDS.delete(aKey);

Also, you don't need to set the property "nickName", because you already used it to create your key. When you need to get the nickname, you do:

user.setNickname(entity.getKey().getName());

UPDATE:

There is a problem in how you create your entity. Do it this way:

Entity entity = new Entity("Gamer2", user.getNickname());
aDS.put(entity);

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