Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update a field value of a data store in google app engine using java

I had created a Login (Kind) in the data store. Inside the login, I created two fields username and passsword. When the user changes the password I want to update the field value inside the datastore. but when I used this code,

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity passwordchange = new Entity("Login");
passwordchange.setProperty("password", "admin@123");
datastore.put(passwordchange);

It creates a new row and inserts the password "admin@123" instead of updating the new password in same row.

Can anyone suggest me how to update the data of a field.

like image 785
sathya Avatar asked Dec 28 '12 13:12

sathya


1 Answers

If you want to update an entity, you have two options:

A. Retrieve this entity from the Datastore by its id. Update property. Put it back into the Datastore.

try {
    loginEntity = datastore.get(KeyFactory.createKey("login", id));
    loginEntity.setProperty("password", "admin@123");
    datastore.put(loginEntity);
} catch (EntityNotFoundException e) {
// This should never happen
}

B. Create a new entity using the same id. Add all properties. Put in a Datastore - it will override the old entity.

Entity loginEntity = new Entity("login", id);
loginEntity.setProperty("password", "admin@123");
datastore.put(loginEntity);

In both examples id is the id of your entity that you want to change.

I hope you do not store passwords as strings.

like image 82
Andrei Volgin Avatar answered Nov 03 '22 09:11

Andrei Volgin