5

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656

3 Answers3

3

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).

  • @Sam, which alternative are you comparing to? Is it more expensive than Thilo's solution? How are you evaluating how expensive this call is? – Price Jun 29 '14 at 14:36
2

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I am not able to make it work: Problem with query – Guido Jan 09 '11 at 22:22
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"));

Serf
  • 61
  • 1
  • 6
  • The doc is correct. The ":p" is your list, and you are checking for objects whose "lastName" is contained in your list, hence .contains(). – opowell Sep 04 '13 at 15:57