简体   繁体   中英

“OR” operator in Google App Engine (Java)

I'm using GAE in Java, I have a data table "Person" and I need doing something like:

SELECT * FROM PERSON WHERE NAME="MIKE" OR AGE=50

My code builds "AND", I don't know how to build the sentence with OR

Query query = new Query(getEntityKind(), getRootEntityKey());
query.addFilter("name", FilterOperator.EQUAL, "Mike");
query.addFilter("age", FilterOperator.EQUAL, 50);

Any ideas?

Thanks!

There is no OR operator in GAE queries. The only way is to run two queries and then merge results.

Update: since 1.7.0, GAE supports OR queries via CompositeFilterOperator . What it does is basically run parallel queries and then AND/OR them on server. The cost is the same as running separate queries and merging them on client, but one saves on roundtrip times.

You can do OR in JDO or JPA - but it's a bit of a trick because it actually translates your query into an IN query. That might give you some ideas on how to rewrite your query though.

Check out the end of this blog post for more: http://gae-java-persistence.blogspot.co.uk/2009/12/queries-with-and-in-filters.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM