Google App Engine

Google App Engine and Google Web toolkit

 

App Engine Datastore – steps on creating JPA objects:

1) Create JPA Objects with appropriate relationship. The JPA objects require the Key class if you are going to use the JPA relationship to create a relational datastore.

2) Follow documents in adding test jars for app engine datastore and write test classes. Initialize the  LocalServiceTestHelper class and then call DAO classes to store and retrieve.

 

Creating a datastore key:

Key modelId = KeyFactory.createKey(ModelClass.class.getSimpleName(),
someModel.getModelLongId());

Make sure that the Key.toString produces the same string such as ClassSimpleName(1). If the toString does not matches then you do not have the same key.

 

How to Query Datastore with key:


protected Object findByKey(Key modelId, Class persistObject) throws Exception {

String modelIdQuery = “Select persitObj from ” + persistObject.getName() + ” persitObj ”
+ ” where persitObj.modelId = :modelId “;

try {
validateEntityManager();

// user name query
Query query = em.createQuery(modelIdQuery);
query.setParameter(“modelId”, modelId);
List resultList = query.getResultList();
// lets see if we got a user already in our db.
if (resultList.size() > 0) {
return resultList.get(0);
}

return null;

}
catch (Exception e) {
throw e;
}
}