开发者

JDOQL Any way to avoid multiple .contains() calls in the query when searching for the presence of one or more elements in a member List variable?

The question pretty much says it all.

If I have a class Class A

public class A {
    ...
    private List<String> keys;
    ...
}

And I want to select all A instances from the DataStore that have atleast one of a List of keys, is there a better way of doing it than this:

query = pm.newQuery(A.class);
query.setFilter("keys.contains(:key1) || keys开发者_如何转开发.contains(:key2) || keys.contains(:key3)");
List<A> results = (List<A>)query.execute(key1, key2, key3);

This has not yet been implemented, so I am open to radical suggestions.


"SELECT FROM " + A.class.getName() + " WHERE keys.contains(var) && (var == :key1 || var == :key2 || var == :key3) VARIABLES java.lang.String var"

Or at least that's what we'd use with other datastores; anyones guess if Google have implemented it.


It's now implemented in gae so your code runs perfectly. Here another example:

Query query = pm.newQuery(Book.class);
query.setFilter("_AuthorKeys.contains(:key1) || _AuthorKeys.contains(:key2)");
Key key1 = KeyFactory.stringToKey("xxxxxxxxxxx");
Key key2 = KeyFactory.stringToKey("yyyyyyyyyyy");
List<Book> books = (List<Book>) query.execute(key1, key2);

or

Query query = pm.newQuery(Book.class);
query.setFilter("_AuthorKeys.contains(:keys)");
List<Key> keys = new LinkedList();
keys.add(KeyFactory.stringToKey("xxxxxx"));
keys.add(KeyFactory.stringToKey("yyyyyy"));
List<Book> books = (List<Book>) query.execute(keys);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜