Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a way to convert appengine datastore Entity to my object?

Using google appengine 1.3.0 w/ java and jdo...

While trying to write JDO querys for 1-to-many owned relationships, I came across a non-JDO concept that I thought was really smart. Ancestor Querys. The appengine.api.datastore.Query interface allows for scoping of a query using the parent Key.

Unfortunately the results from the query are 'Entity' objects with property lists. Is there a util in the apis that will convert one of these Entity objects into my JDO object or even a simple DTO bean (that matched my JDO object)?

I've taken a crack a brute forcing it with the code below but don't like the double lookup.

 PersistenceManager pm;
 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();  
 List<MyObject> results;

 com.google.appengine.api.datastore.Query query = new Query( "MyObject", KeyFactory.stringToKey( parentId ) );
 query.addFilter("rank", Query.FilterOperator.GREATER_THAN_OR_EQUAL, minRank );
 query.addSort("rank");
 query.setKeysOnly();
 for (Entity anEntity : datastore.prepare(query).asIterable()) {
  results.add( pm.getObjectById( MyObject.class, anEntity.getKey() ) );
 }
like image 926
Stevko Avatar asked Feb 18 '10 18:02

Stevko


2 Answers

you can use org.datanucleus.store.appengine.JDODatastoreBridge.toJDOResult()

like image 92
maximbr Avatar answered Nov 14 '22 22:11

maximbr


This is not the silver bullet I think you were looking for; it requires some grunt work to accomplish.

DAO code

DatastoreService datastore = DatastoreServiceFactory
 .getDatastoreService();
List<Foo> results = new ArrayList<Foo>();

Query query = new Query("Foo", KeyFactory.stringToKey(""));
List<Entity> entities = datastore.prepare(query).asList(
  FetchOptions.Builder.withDefaults());

for (Entity entity : entities) {
  results.add(new Foo(entity));
}

class Foo

public Foo(Entity entity) {
  // TODO get properties from entity and populate POJO
  this.bar=entity.getProperty("bar");
  //get the key
  //if the @PrimaryKey is a Long use this
  this.id=entity.getKey().getId();
  //if the @PrimaryKey is a String use this
  this.id=entity.getKey().getName();
}
like image 20
antony.trupe Avatar answered Nov 14 '22 22:11

antony.trupe