views:

42

answers:

2

How do you query for a list of objects stored using GAE, where a Set field contains a specified string? ie Imagine this imaginary example:

@PersistenceCapable
class Photos {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    String name = "";
    Set<String> tag = new Hashset<String>();
}

Assuming there are 40,000 photos in the "database", How do I Query all photos where tag='2009'

Or another example from google's documentation, if you have the following class:

@PersistenceCapable
public class Person {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private Set<Key> favoriteFoods;

    // ...
}

How do you fetch a list of all Person objects where they have a specific favourite food Key?

+2  A: 

How would you do that in Java ? collField.contains(:stringParam)

DataNucleus
+1  A: 

Try something like this, which is basically taken from the google documentation:

Query query = pm.newQuery("select from com.example.Note where tag == tagname order by created");
query.declareParameters("String tagname");
return (List<Note>) query.execute(tag);

As per the comment below, this also seems to work:

Query query = pm.newQuery("select from com.example.Note where tags.contains(tagname) order by created");
query.declareParameters("String tagname");
return (List<Note>) query.execute(tag);
Jacob
Please don't try that if you want your query to be JDO compliant. You cannot do "Collection == String" in Java so you cannot in JDOQL. You use "contains(...)"
DataNucleus