Tuesday, 6 December 2011

Using JDO with Google AppEngine Datastore

Today we’ll walk through on steps how to perform an insert into Google AppEngine datastore using Datastore API and JDO.
In my last project I used Google AppEngine datastore without JDO or JPA. It was fun. Well, I had to do a lot of transformations on my own from Entities to POJOs. Something like the following:
   1: Entity brandEntity = new Entity(Brand.class.getSimpleName());
   2: brandEntity.setProperty("name", brand.getName());
   3: brandEntity.setProperty("description", brand.getDescription());

With JDO you don’t have to worry about it. You need to worry about your Java objects and not Datastore entities. So, no more string literals and you are all set with type safety. The above code will look similar to something like the following:


   1: Brand brand = new Brand();
   2: brand.setName("Xbox 360");
   3: brand.setDescription("Gaming console by Microsoft");

Using Datastore API you insert you data using the following:


   1: DatastoreService datastore = Datastore.get();
   2: Entity brandEntity = new Entity(Brand.class.getSimpleName());
   3: brandEntity.setProperty("name", brand.getName());
   4: brandEntity.setProperty("description", brand.getDescription());
   5: brandEntity.setProperty("manufacturerId", brand.getManufacturerId());        
   6: datastore.put(brandEntity);

Datastore.java is a custom class defined as:


   1: public class Datastore {
   2:     private static final DatastoreService datastore = 
   3:             DatastoreServiceFactory.getDatastoreService();
   4:  
   5:     private Datastore() {
   6:     }
   7:  
   8:     /**
   9:      * singleton instance of the datastore service
  10:      * 
  11:      * @return
  12:      */
  13:     public static DatastoreService get() {
  14:         return datastore;
  15:     }
  16: }

Using JDO, the same can be achieved by the following:


   1: PersistenceManager db = Persistancemanager.getPersistenceManager();
   2: Brand brand = new Brand();
   3: brand.setName("Xbox 360");
   4: brand.setDescription("Gaming console by Microsoft");
   5: try {
   6:     db.makePersistent(brand);
   7: } finally {
   8:     db.close();
   9: }

Persistencemanager.java is a custom class that is a wrapper over JDO PersistenceManager defined as following:


   1: public final class Persistancemanager {
   2:     private static final PersistenceManagerFactory pmfInstance = 
   3:         JDOHelper.getPersistenceManagerFactory("transactions-optional");
   4:  
   5:     private Persistancemanager() {
   6:     }
   7:  
   8:     public static PersistenceManagerFactory getInstance() {
   9:         return pmfInstance;
  10:     }
  11:  
  12:     public static PersistenceManager getPersistenceManager() {
  13:         return pmfInstance.getPersistenceManager();
  14:     }
  15: }

The Google AppEngine Datastore is schemaless. JDO and/or JPA provides a way to add a soft schema layer on top of the datastore. So, apart from the benefit of CRUD-ing data easily we can also enforce a lot of application level constraints like association between entities and so on.

I am sure the above is going to help a lot of beginners and Google AppEngine enthusiasts in getting upto speed with the datastore.