We can use JPA while dealing with Object Store in Google App Engine, this abstracts the interaction with object store.
Because both Object store and JPA are object-based there is good matching between both technologies.
Here is the simple steps we need to follow:
1) Create the JPA beans
- Including annotation
- Define primary key
- Setters/Getters for fields.
Here is an example of using Locate object as JPA bean:
@PersistenceCapable(identityType=IdentityType.DATASTORE)
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Long id;
@Persistent
public int entityId;
@Persistent
public double lat;
@Persistent
public double lon;
@Persistent
public double accuracy;
@Persistent
public long time;
public Location(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
...
... //getters and setters
}
We used auto-generation primary key strategy here.
2) Create session facade or abstraction layer or DAO object to interact with this bean
public class LocationDAO {
}
3) Define PersistenceManagerFactory object
private static final PersistenceManagerFactory persistenceFactory = JDOHelper
.getPersistenceManagerFactory("transactions-optional");
4) Get PersistenceManager object to use for JPA interactions
PersistenceManager pm = pmfInstance.getPersistenceManager();
5) Example for ADD/CREATE operation
public Location addLocation(int entityId, double lat, double lon, double accuracy,long time){
Location location = null;
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
location=new Location();
location.setEntityId(entityId);
location.setLat(lat);
location.setLon(lon);
location.setAccuracy(accuracy);
location.setTime(time);
pm.makePersistent(location);
} finally {
pm.close();
}
return location;
}
6) Example for DELETE operation
public void removeLocation(Location location) {
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
pm.currentTransaction().begin();
location = pm.getObjectById(Location.class, location.getId());
pm.deletePersistent(location);
pm.currentTransaction().commit();
} catch (Exception ex) {
pm.currentTransaction().rollback();
throw new RuntimeException(ex);
} finally {
pm.close();
}
}
7) Example of UPDATE operation
public void updateLocation(Location location,int entityId, double lat, double lon, double accuracy,long time) {
PersistenceManager pm = persistenceFactory.getPersistenceManager();
try {
pm.currentTransaction().begin();
location = pm.getObjectById(Location.class, location.getId());
location.setEntityId(entityId);
location.setLat(lat);
location.setLon(lon);
location.setAccuracy(accuracy);
location.setTime(time);
pm.makePersistent(location);
pm.currentTransaction().commit();
} catch (Exception ex) {
pm.currentTransaction().rollback();
throw new RuntimeException(ex);
} finally {
pm.close();
}
}
8) Example of SELECT operation
public List
getAllLocationsForEntity(int entityId){
PersistenceManager pm = persistenceFactory.getPersistenceManager();
String queryStr = "select from " + Location.class.getName();
Query selectQuery=pm.newQuery(queryStr);
selectQuery.setFilter("entityId=="+entityId);
return (List
) selectQuery.execute();
}
These are the simple basic CRUD operations to perform on Object data store in Google App Engine using JPA.