Reputation: 8135
Write to datastore
Key dataKey = KeyFactory.createKey("Datastore", "123");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity data = new Entity("Datastore", dataKey);
data.setProperty("date", date);
try{
datastore.get(dataKey);
datastore.delete(dataKey);
}catch(EntityNotFoundException ex){
log.serve("Error : "+ ex.getMessage());
}
datastore.put(data);
Read from datastore
Key dataKey = KeyFactory.createKey("Datastore", "123");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity entity = datastore.get(dataKey);
I have 02 questions:
1. I use datastore.get(dataKey) to check whether the entity is existed or not, because I want to avoid duplicated entity with same key but I don't think the way i'm using is good. Is there any other way can do it better?
2. I can't get data back. It said that " No entity was found matching the key: Datastore("123")"
I read google datastore documentation again and again but I still can't find out what wrong with this :(.
Can somebody help me please ?
Thank you.
Upvotes: 0
Views: 257
Reputation: 31928
Replace:
Entity data = new Entity("Datastore", dataKey)
with:
Entity data = new Entity(dataKey)
Also you don't need to worry about duplicates, if you commit an existing entry (which its key is already in the datastore) it will be override. There can't be two same keys in the datastore.
Upvotes: 1