views:

41

answers:

1

I have a many-to-many relationship. Just like in the example from the documentation:

Person.java
    private Set<Key> favoriteFoods;

Food.java
    private Set<Key> foodFans;

How do I get all "favorite foods" of a certain "food fan", if i have retrieved a "Person" object and i have the favoriteFoods key set. Is there a better way than:

for (Key k: favoriteFoods)
{ foodObjectsCollection.add( pm.getObjectById(Food.class, k) ); }

What's the cheapest and most efficient option here? I usually have to generate tables with object data in my app.

+1  A: 

It seems like you need JDOQL. For this concrete task try:

Query query = pm.newQuery(Food.class, ":p.contains(key)");
query.execute(favoriteFoods);

contains() behaves just like IN statement in SQL, so this query will fetch all Food objects from specified key set.

Andrei
In terms of quota and limitations, couple short questions:1. Does every getObjectById in my method is going to use 1 API call?2. Using your method, do i have any limitations like.. max 5000 entries i can execute this JDOQL query on?
mateusz
1) yes, every call from Java to datastore is treated as separate API call. You will have to make complex queries by hand to pass it around. 2) as far as I know, there's no other limitations except those which are listed in their [Quotas](http://code.google.com/appengine/docs/quotas.html) section.
Andrei