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.

Tags: , , , ,

2 comments

  1. Javier Fernandez

    Unfortunately, getObjectsById iterates for each Key and retrieves ever object individually, thus making a DataStore.GET operation for every object.

    I’m can’t find how to do a bulk fetch of the objects, such as done with the low-level API. I have read somewhere that the newest Experimental version of JDO support would make getObjectsById more efficient.

    Do you have any suggestions to solve this problem?

  2. Seems like getById will always have to be single query as it means you need to retrieve a specific object. The best optimization that can be done here is to make the queries in parallel (which I think JDO might be doing in the case of getObjectsById though not sure)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>