Updating a model

To update a model, fetch the model, modify it, and put it to the datastore:
public void updateEmployeeTitle(Key key, String newTitle) {
   
Employee emp = Datastore.get(Employee.class, key);
    emp
.setTitle(newTitle);
    Datastore.put(emp);

}

You can update multiple models as follows. This method is more efficient than a series of individual Datastore.put() invocations.
public void updateEmployeesTitle(List<Key> keys, String newTitle) {
   
List<Employee> list = Datastore.get(Employee.class, keys);
for (Employee emp : list) {
    emp.setTitle(newTitle);
}
    Datastore.put(list);
}

You can update a model asynchronously as follows:
Employee emp = Datastore.get(Employee.class, key);
emp
.setTitle(newTitle);
Future<Key> future = Datastore.putAsync(emp);
// Do something
...
// Get the result
Key key = future.get();

Next...

Continue to Deleting a model.


Comments