简体   繁体   中英

How to remove a row from Google Datastore using Java?

I want to remove a row entry from google datastore.

I have coded :

            String[] 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 (ie, user name?) is the key name.

Per the getKey() documentation:

If the entity has not yet been saved (eg 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.

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