views:

311

answers:

3

I have about 50k entities stored in appengine. I am able to look up an individual record via the GQL admin interface with a query like:

SELECT * FROM Pet where __key__ = KEY( 'Pet','Fido')

But I'm having trouble figuring out how to do a batch version of this via JDO. Right now I have this:

    PersistenceManager pm = ...;
    for(Pet pet : pets) {
        for(String k : getAllAliases(pet)) {
            keys.add(KeyFactory.createKeyString(Pet.class.getSimpleName(), k));
        }
    }
    Query q = pm.newQuery("select from " + Pet.class.getName() + " where id == :keys");
    List<Pet> petlist = (List<Pet>) q.execute(keys);

But though 'Fido' works in the GQL case, it returns nothing when I use that Java + JDO code. What am I doing wrong?

+1  A: 

Likely doesn't work in the JDOQL case since it isn't valid JDOQL :-P

Collection keys = ... (create your keys collection)
Query q = pm.newQuery("SELECT FROM " + Pet.class.getName() + " WHERE :keys.contains(id)");

i.e the filter uses Java syntax

DataNucleus
Good to know. I was expecting to get some sort of hard-to-ignore error notification if the query wasn't valid, so I'm disappointed there.
TheDon
A: 

Don't use queries to fetch entities by key - they're inefficient, and require a query for each object retrieved! Instead, get the objects by key. Bulk fetches by key don't appear to be supported by JDO, but fetching them by key one at a time is still going to be a lot more efficient than doing queries for them.

Nick Johnson
I find it hard to believe that fetching entities - synchronously - individually by key is going to be faster than doing a JDO query. I will try the experiment at some point.
TheDon
'IN' is not natively supported by App Engine - the SDK emulates it by executing multiple, sequential, queries. Each query has to do an index scan followed by a lookup by key. In comparison, a lookup by key only has to do a small part of that, so it's substantially faster.
Nick Johnson
A: 

http://gae-java-persistence.blogspot.com/2009/10/executing-batch-gets.html

I didn't try the JDO version, but the JPA version does work!

Kenyth