Appengine: Check if object is present in database


/ Published in: Java
Save to your folder(s)

import com.google.appengine.api.datastore.Key;\\\\\\\\\\\\\\\\r\\\\\\\\\\\\\\\\nimport com.google.appengine.api.datastore.KeyFactory;


Copy this code and paste it in your HTML
  1. PersistenceManager pm = JDOHelper.getPersistenceManagerFactory("transactions-optional").getPersistenceManager();
  2. String keyAsString = "key of object";
  3. Key objectDbKey = KeyFactory.createKey(MyDbObject.class.getSimpleName(), keyAsString);
  4. DbObject_Device object = null;
  5. try{
  6. object = pm.getObjectById(MyDbObject.class, objectDbKey);
  7. }
  8. if(object != null){
  9. // Object was present in database
  10. } else{
  11. // .. not present
  12. object = new DbObject();
  13. }
  14. // change object values here
  15. pm.makePersistent(object );
  16. pm.close();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.