I set up method to query database using WHERE IN, but the query performs opposite of how I expect.
CriteriaBuilder qb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<ActivityLog> c = qb.createQuery(ActivityLog.class);
Root<ActivityLog> p = c.from( ActivityLog.class );
Predicate condition1 = qb.equal( p.get(ActivityLog_.div), div);
Collection<String> viewStatus = new ArrayList<String>();
viewStatus.add("C");
viewStatus.add("U");
viewStatus.add("D");
Predicate condition2 = qb.equal(p.get(ActivityLog_.status), p.get(ActivityLog_.status).in(viewStatus));
c.where( qb.and(condition1, condition2) );
c.orderBy( qb.desc( p.get( ActivityLog_.update_time.getName() ) ) );
TypedQuery<ActivityLog> q = getEntityManager().createQuery(c);
q.setMaxResults(15);
return q.getResultList();
The results are actually the opposite — I get STATUS the are NOT C,U,D.
If I change the Predicate condition2 line — I get the results that I expect??
Predicate condition2 = qb.notEqual(p.get(ActivityLog_.status), p.get(ActivityLog_.status).in(viewStatus));
Is there a bug with MySQL driver or Eclipselink? or a flaw in my thinking?
My hope is to retrieve from ActiivtyLog where divsion = div and status
IN (C,U,D)
Why are you using qb.equal(p.get(ActivityLog_.status), yourInCondition)? The in condition should return a true or false, so i don’t see why you would want to check that it equals your status.
In EclipseLink you can turn Logging to Fine or Finest to see the SQL that gets generated, which makes it easier to tell how your queries might be going wrong.
What I think you might want instead is just
which can also be done as :