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));
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 = :keywordspart of your query is false on records with nullkeywords— since that part is false, so is theand, of course.You’ll need two queries, one for
keywords = :keywordsand 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, notOR; the Java equivalent of that app-level-simulatedINis actuallycontains.