即使“keywords”值为空,如何更改此查询以使其返回满足“minPrice”条件的记录?
我不知道这是否是 Google App Engine for Java 特有的问题,但如果设置为 keywords
参数的值是空字符串,则查询不会返回任何内容,即使如果设置了 minPrice
。
即使 keywords
值为 null,如何更改此查询以使其返回满足 minPrice
条件的记录?理想情况下,我会以某种方式对这两个条件使用相同的查询,而无需基于空字符串条件创建单独的查询。
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));
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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 GAE 数据存储的工作方式(顺便说一句,大多数关系数据库也以这种方式工作!):空值不等于任何内容,因此您的
keywords = :keywords
部分对于包含 nullkeywords
的记录,查询为 false - 因为该部分为 false,所以and
当然也是如此。您需要两个查询,一个用于
keywords = :keywords
,另一个用于“is null”检查,并使用它们的两个不相交的结果集(Python GAE 在应用程序级别模拟“IN”运算符代码,我相信 Java GAE 不会,但由于在这种情况下集合是不相交的,所以无论如何它都没有什么神秘或困难;-)。编辑:它是 Python 中的模拟
IN
(此处可用),不是OR
; Java 中应用程序级模拟的IN
等效项实际上是contains
。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 nullkeywords
-- since that part is false, so is theand
, 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, notOR
; the Java equivalent of that app-level-simulatedIN
is actuallycontains
.