Reputation: 437
Got an entity A with like this
public class A
private String a;
private String b;
..
..
..
How do I write a JDO query that select all the A objects that matches either keyword==a OR keyword ==b
Full code looks something like this:
Query q = pm.newQuery(SELECT FROM A WHERE a == keyword || b == keyword ORDER BY date DESC");
List<A> results = (List<A>)q.execute(keyword);
This code does not give any error but does not give any results. Removing the || part gives results.
Thanks
Upvotes: 0
Views: 479
Reputation: 14187
You can't do an OR on two different fields. Here is a snippet from the docs:
In the JDOQL string syntax, you can separate multiple filters with || (logical "or") and && (logical "and"), although keep in mind that || can only be employed when the filters it separates all have the same field name. In other words, || is only legal in situations where the filters it separates can be combined into a single contains() filter:
// legal, all filters separated by || are on the same field
Query query = pm.newQuery(Employee.class,
"(lastName == 'Smith' || lastName == 'Jones')" +
" && firstName == 'Harold'");
// not legal, filters separated by || are on different fields
Query query = pm.newQuery(Employee.class,
"lastName == 'Smith' || firstName == 'Harold'");
One way around this might be to store your a and b in a list. If you have a list of items called foo, you could do a query where foo = keyword, and if any item in foo matches, you will get that object back in your results. You don't say much about what a and b are, so I don't know if this will work for you or not :)
Update:
public class Example {
String firstName;
String lastName;
List<String> allNames;
public Example(String first, String last){
firstName = first;
lastName = last;
allNames = new ArrayList<String>();
allNames.add(first);
allNames.add(last);
}
}
With something like that, you could then do a query where "allNames == 'Smith' || allNames=='Jones'".
Upvotes: 2