Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getObjectsById on App Engine

Tags:

According to JDO, you can use PersistenceManager.getObjectsById to load multiple entity instances by their object id.

What kind of Collection does one need to use here? A Google Data Store Key does not work as object id.

like image 522
Thilo Avatar asked Jan 07 '10 01:01

Thilo


2 Answers

Use PersistenceManager.newObjectIdInstance(), as such

List<Object> ids = new ArrayList<Object>();
for (Key key : keys) {
   ids.add(pm.newObjectIdInstance(Foo.class, key));
}

return (List<Foo>) pm.getObjectsById(ids);

I'm not sure however how expensive the call to newObjectIdInstance is (it shouldn't be from what I can see).

like image 90
Erik Ogenvik Avatar answered Sep 25 '22 16:09

Erik Ogenvik


Not a direct answer, by as an alternative to getObjectsById, it seems that you can use a JDOQL query to load multiple entities by key:

public List getById(List keys) {
   Query q = pm.newQuery(
      "select from " + Book.class.getName() + " where :keys.contains(key)");
   return (List) q.execute(keys);
}

Apparently, this query is optimized to use an efficient low-level bulk API.

The order of the keys does get lost though, so you will have to re-sort the result in Java land.

like image 33
Thilo Avatar answered Sep 22 '22 16:09

Thilo