如何使用java从谷歌应用引擎数据存储中加载OR操作的值

时间:2015-05-10 10:07:45

标签: java google-app-engine google-cloud-datastore

我必须使用OR条件使用2个条件来加载实体值 例如,SELECT * FROM db WHERE (email = xyz@gmail.com OR UUID = 1234)

尝试使用过滤器:

Filter emailFilter =
              new FilterPredicate("email", FilterOperator.EQUAL, email);

Filter firstFilter =
              new FilterPredicate("firstname", FilterOperator.EQUAL, firstName);

Filter filter =
              CompositeFilterOperator.or(emailFilter, firstFilter);

但我不明白如何在加载时使用此过滤器...

如何使用数据存储区中的过滤器执行此操作?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只需将过滤器应用于查询并检索结果:

// Your filter declarations
Filter emailFilter =
    new FilterPredicate("email", FilterOperator.EQUAL, email);
Filter firstFilter =
    new FilterPredicate("firstname", FilterOperator.EQUAL, firstName);
Filter filter =
    CompositeFilterOperator.or(emailFilter, firstFilter);

// Use class Query to assemble a query
Query q = new Query("Person").setFilter(filter)

// Use PreparedQuery interface to retrieve results
PreparedQuery pq = datastore.prepare(q);

for (Entity result : pq.asIterable()) {
    // result.getProperty("lastname")
}