Jpa QueryBuilder where 子句中的多个表达式不起作用
我在使用 javax.persistence.criteria.CriteriaBuilder 创建查询时遇到问题。我正在使用 EclipseLink 2.1 和 Oracle 10g 数据库。当构建具有多个限制的查询时,它将仅使用第一个限制,而不是两者都使用。
这是我的代码:
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<Assignment> query = cb.createQuery(Assignment.class);
Root<Assignment> assignment = query.from(Assignment.class);
query.where(
cb.equal(assignment.get("request"), request),
cb.isNull(assignment.get("endDate")));
return getEm().createQuery(query).getResultList();
生成的查询是:
SELECT ASSX_ID, END_DATE, BEGIN_DATE, COMMENTS,
ASSX_OER_ASSIGNED_TO_ID, OER_OER_ID_ASSIGNED_BY,
ASSX_RQST_ID
FROM TARTS.ASSIGNMENT_XREF
WHERE (ASSX_RQST_ID = ?)
除了 where 子句之外,它看起来不错。我期待:
SELECT ASSX_ID, END_DATE, BEGIN_DATE, COMMENTS,
ASSX_OER_ASSIGNED_TO_ID, OER_OER_ID_ASSIGNED_BY,
ASSX_RQST_ID FROM TARTS.ASSIGNMENT_XREF
WHERE (ASSX_RQST_ID = ? AND BEGIN_DATE IS NOT NULL)
是否使用 cb.and(arg1, arg2) 并不重要。我做错了什么吗?任何帮助将不胜感激。
I am having an issue with creating queries with javax.persistence.criteria.CriteriaBuilder. I am using EclipseLink 2.1 and an Oracle 10g database. When building a query with multiple restrictions it will use only the first restriction, not both of them.
Here is my code:
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<Assignment> query = cb.createQuery(Assignment.class);
Root<Assignment> assignment = query.from(Assignment.class);
query.where(
cb.equal(assignment.get("request"), request),
cb.isNull(assignment.get("endDate")));
return getEm().createQuery(query).getResultList();
The query producted is:
SELECT ASSX_ID, END_DATE, BEGIN_DATE, COMMENTS,
ASSX_OER_ASSIGNED_TO_ID, OER_OER_ID_ASSIGNED_BY,
ASSX_RQST_ID
FROM TARTS.ASSIGNMENT_XREF
WHERE (ASSX_RQST_ID = ?)
It looks good except for the where clause. I am expecting:
SELECT ASSX_ID, END_DATE, BEGIN_DATE, COMMENTS,
ASSX_OER_ASSIGNED_TO_ID, OER_OER_ID_ASSIGNED_BY,
ASSX_RQST_ID FROM TARTS.ASSIGNMENT_XREF
WHERE (ASSX_RQST_ID = ? AND BEGIN_DATE IS NOT NULL)
It doesn't matter if I use cb.and(arg1, arg2) or not also. Am I doing something wrong? Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的查询看起来完全没问题。正如您所提到的,
CriteriaQuery.where(Predicate...restrictions)
已经使用了谓词的连接,因此无需使用 cb.and()。我唯一能想象到的事情是:
endDate
可能永远不会为空?Your query looks perfectly ok. As you've mentioned,
CriteriaQuery.where(Predicate... restrictions)
already uses the conjunction of the predicates so there's no need to use cb.and().The only things I could imagine:
endDate
may never be null?