如何在 JPA 中创建虚假谓词
有没有办法在 JPA 中创建虚假谓词? 有点像这样:
CriteriaBuilder cb = em.getCriteriaBuilder;
Predicate pred = cb.isTrue(false);
CriteriaBuilder 的几乎所有方法都以 Expression 作为参数。 我也尝试过这个,但没有成功:
Expression<Object> path = cb.coalesce.value(null);
Predicate pred = cb.isNotNull(path);
显然它会抛出 NPE,但是我认为这可能有效,因为根据 API 文档:
合并表达式相当于 case 表达式,如果其所有参数的计算结果都为 null,则返回 null,否则返回其第一个非 null 参数的值。
Is there any way to create bogus Predicate in JPA?
Sort of like this:
CriteriaBuilder cb = em.getCriteriaBuilder;
Predicate pred = cb.isTrue(false);
Almost all the methods of CriteriaBuilder take Expression as parameter.
I also tried this to no avail:
Expression<Object> path = cb.coalesce.value(null);
Predicate pred = cb.isNotNull(path);
Obviously it throws NPE, however I thought that this might work, because according to API documentation:
A coalesce expression is equivalent to a case expression that returns null if all its arguments evaluate to null, and the value of its first non-null argument otherwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Eclipselink 2.4 中更复杂的查询中,
dijunction
对我不起作用。所做的是 cb.isTrue(cb.literal(false)) ,它是 false 的最精确表示。
dijunction
did not work for me in more complicated query in Eclipselink 2.4.What did was
cb.isTrue(cb.literal(false))
which is as precise representation of false as one can get.我认为 析取 是什么你正在寻找。默认情况下它是 false,直到某个真正的谓词被
or
到它为止。I think a disjunction is what you're looking for. It's false by default, until some real predicate is
or
ed to it.