I'm trying to build the following query with 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();
Basically, I want wo check if there is at least one ProblemBean
which has the given user as mainUserLv
or as member of usersLv
collection.
The generated SQL code is:
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)))
Needless to say this returns always 0. I would expect the following 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))))
Is this a Eclipselink bug or do I miss something?