22
Jun 11

Fetch Multiple Objects by key in JDO using getObjectsById

On Google Appengine I needed to fetch multiple objects, the key to which is present with me. I tried using PersistenceManager#getObjectsById(Collection) method and passed a list of keys to it. But that threw an exception. On further probing I realized this is not how #getObjectsById works.

There might be other ways of doing it but this is how I’ve done it :

List list = new ArrayList();

for(Key key : <iterate-over-list-of-keys>){

list.add(persistenceManager.newObjectIdInstance(<class-name-here>, key));

}

Collection objects = persistenceManager.getObjectsById(list);

Hopefully this helps you use the getObjectsById method in JDO on google appengine.