0

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.

Gopi
  • 10,073
  • 4
  • 31
  • 45
  • You may get some more help in addition to @Andrei's answer from my old question http://stackoverflow.com/questions/3474056/what-does-p-mean-in-a-jdo-query – Gopi Oct 20 '10 at 06:42

1 Answers1

1

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.

ffriend
  • 27,562
  • 13
  • 91
  • 132
  • 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 Oct 20 '10 at 08:53
  • 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. – ffriend Oct 20 '10 at 14:30