views:

31

answers:

1

I don't know if this is a problem that is specific to Google App Engine for Java, but if the value set as the keywords parameter is a null String, then nothing is returned from the query, even if a minPrice is set.

How do I change this query to make it return records that meet the minPrice condition even if the keywords value is null? Ideally I would somehow use the same query for both conditions without creating separate queries based on a null String condition.

Query qry = entityManager.createQuery("SELECT p FROM Test p 
 WHERE keywords = :keywords and price >= :minPrice");

qry.setParameter("keywords", keywords);
qry.setParameter("minPrice", Integer.parseInt(minPrice));
+2  A: 

It's the way the GAE datastore works (most relational databases work that way too, btw!): nulls are not equal to anything, so the keywords = :keywords part of your query is false on records with null keywords -- since that part is false, so is the and, of course.

You'll need two queries, one for keywords = :keywords and one for the "is null" check, and use their two disjoint result sets (Python GAE simulates an "IN" operator in app-level code, which I believe Java GAE doesn't, but since the sets are disjoint in this case there's really no mystery or difficulty to it anyway;-).

Edit: it's a simulated IN (which would be usable here) in Python, not OR; the Java equivalent of that app-level-simulated IN is actually contains.

Alex Martelli
Hmm, my understanding and experience (in the Python runtime) is that you can query for entities whose value for this property is `None` (just like any other value). The only thing you cannot query for is entities which *omit* a property entirely.
David Underhill
@David, true, there _is_ a distinction (in the underlying datastore and therefore in both languages) between an absent field (unsearchable) and one that's null (`None` in Python), which _is_ searchable.
Alex Martelli
Ah, I think I understand the question now - I didn't realize he wanted it to match the a non-null `keywords` parameter *or* `null`. Oops :p.
David Underhill