How to remove a row from Google Datastore using Java?
I want to remove a row entry from google datastore.
I have coded :
Str开发者_如何学Pythoning[] elements = deletedRow.split(",");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity row = new Entity("Row");
Key rowKey;
for (String element : elements) {
row.setProperty("userName", "kapil.kaisare");
out.print("UserName : " + element);
row.setProperty("description", element);
rowKey = row.getKey();
out.print("\nKey : " + rowKey);
datastore.delete(rowKey);
}
deletedRow is a query parameter coming from AJAX javaScript call & that's not empty for sure.
Username printed successfully. While Key prints :
Key : Key 0
This is surprising me ! Why is there key 0 even if I am setting row properties !
Please suggest some solution.
For reference : Queries and Indexes, Entities, Properties, and Keys
Try the following
String[] elements = deletedRow.split(",");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
for (String element : elements) {
Key key = KeyFactory.createKey("Row", element);
datastore.delete(key);
}
I assume Row is the kind of entity and element (i.e., user name?) is the key name.
Per the getKey()
documentation:
If the entity has not yet been saved (e.g. via DatastoreService.put), this Key will not be fully specified and cannot be used for certain operations (like DatastoreService.get). Once the Entity has been saved, its Key will be updated to be fully specified.
As such, you should not attempt to use the key for entities that have never been saved to the datastore, unless you specified a full key in the entity's constructor.
精彩评论