getObjectsById on App Engine
According to JDO, you can use PersistenceManager.getObjectsById to load mult开发者_C百科iple 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.
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).
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.
The answer above me is almost correct.
There seems to be a mistake in the syntax explained by Google on their developers website.
Explained by google:
// Give me all Employees with lastName equal to Smith or Jones Query query = pm.newQuery(Employee.class, ":p.contains(lastName)"); query.execute(Arrays.asList("Smith", "Jones"));
Surely it should be:
// Give me all Employees with lastName equal to Smith or Jones Query query = pm.newQuery(Employee.class, "p.contains(:lastName)"); query.execute(Arrays.asList("Smith", "Jones"));
精彩评论