我正在尝试使用JPA Criteria API(Eclipselink)构建以下查询:
final CriteriaBuilder qb = this.em.getCriteriaBuilder();
final CriteriaQuery<Long> cq = qb.createQuery(Long.class);
final Root<ProblemBean> problem = cq.from(ProblemBean.class);
cq.select(qb.countDistinct(problem));
cq.where(qb.or(qb.equal(problem.get(ProblemBean_.mainUserLv), user), qb.isMember(user, problem.get(ProblemBean_.usersLv))));
return this.em.createQuery(cq).getSingleResult();
基本上,我想检查是否至少有一个ProblemBean
具有给定用户mainUserLv
或usersLv
集合的成员。
生成的SQL代码是:
SELECT COUNT(DISTINCT(t0.ID)) FROM T_PROBLEM t0, T_PROBLEM_USER t2, T_USER t1
WHERE (((t0.ID_USER_LV = ?) OR (t1.ID = ?)) AND
((t2.ID_PROBLEM = t0.ID) AND (t1.ID = t2.ID_USER)))
不用说这总是返回0。 我希望以下SQL:
SELECT COUNT(DISTINCT(t0.ID)) FROM T_PROBLEM t0, T_PROBLEM_USER t2, T_USER t1
WHERE ((t0.ID_USER_LV = ?) OR
((t1.ID = ?) AND ((t2.ID_PROBLEM = t0.ID) AND (t1.ID = t2.ID_USER))))
这是一个Eclipselink错误还是我错过了什么?