根据 JDO,您可以使用PersistenceManager.getObjectsById通过对象 id 加载多个实体实例。
这里需要使用什么样的 Collection?Google 数据存储密钥不能用作对象 ID。
根据 JDO,您可以使用PersistenceManager.getObjectsById通过对象 id 加载多个实体实例。
这里需要使用什么样的 Collection?Google 数据存储密钥不能用作对象 ID。
像这样使用 PersistenceManager.newObjectIdInstance()
List<Object> ids = new ArrayList<Object>();
for (Key key : keys) {
ids.add(pm.newObjectIdInstance(Foo.class, key));
}
return (List<Foo>) pm.getObjectsById(ids);
但是我不确定对 newObjectIdInstance 的调用有多昂贵(它不应该是我所看到的)。
不是直接的答案,作为替代方案getObjectsById
,您似乎可以使用JDOQL 查询按键加载多个实体:
public List getById(List keys) {
Query q = pm.newQuery(
"select from " + Book.class.getName() + " where :keys.contains(key)");
return (List) q.execute(keys);
}
显然,此查询经过优化以使用高效的低级批量 API。
但是,键的顺序确实会丢失,因此您必须在 Java 领域重新排序结果。
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"));