How to convert appengine Entity storage object into an object?

Using google appengine 1.3.0 with java and jdo ...

When trying to write JDO queries for a one-to-many relationship, I came across a different concept from JDO, which I thought was very smart. Requests of the ancestors. The appengine.api.datastore.Query interface allows you to view a query using the parent key.

Unfortunately, the query results are Entity objects with property lists. Is there a utility in apis that converts one of these Entity objects to my JDO object or even a simple DTO bean (which matches my JDO object)?

I cracked the rough one by forcing it with the code below, but I don't like the double search.

 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() ) );
 }
+5
2

, , ; .

DAO

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));
}

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();
}
+4

org.datanucleus.store.appengine.JDODatastoreBridge.toJDOResult()

+5

All Articles