5

根据 JDO,您可以使用PersistenceManager.getObjectsById通过对象 id 加载多个实体实例。

这里需要使用什么样的 Collection?Google 数据存储密钥不能用作对象 ID。

4

3 回答 3

3

像这样使用 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 的调用有多昂贵(它不应该是我所看到的)。

于 2011-02-02T21:41:24.107 回答
2

不是直接的答案,作为替代方案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 领域重新排序结果。

于 2010-01-07T02:06:55.270 回答
0

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

于 2012-12-27T13:36:16.630 回答