Simple JPA 2 criteria query "where" condition
criteria, jpa, jpa-2.0
Solution
Yes, you have to use `cq.where()`.
Try something like this:
Root<Utente> utente = cq.from(Utente.class);
boolean myCondition = true; // or false
Predicate predicate = cb.equal(utente.get(Utente_.ghost), myCondition);
cq.where(predicate);
Where I have used the canonical metamodel class `Utente_` that should be generated automatically. This avoids the risk of making errors in typing field names, and enhances type safety. Otherwise you can use
Predicate predicate = cb.equal(utente.get("ghost"), myCondition);
Problem
I'm learning jpa-hibernate basics. I have this query for getting all users: ``` CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); cq.select(cq.from(Utente.class)); return getEntityManager().createQuery(cq).getResultList(); ``` Now I want to filter by a boolean field named 'ghost' where it equals true (or false, it depends). Translated: SELECT * FROM users WHERE ghost = 0; Do I have to use cq.where() ? How?