2012-05-14 3 views
3

jpa-hibernate 기본 사항을 배우고 있습니다.간단한 JPA 2 기준 쿼리 "where"조건

나는 모든 사용자를 얻기를 위해이 쿼리를 가지고 :

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); 
     CriteriaQuery cq = cb.createQuery(); 
     cq.select(cq.from(Utente.class)); 
     return getEntityManager().createQuery(cq).getResultList(); 

가 지금은 그것이 사실 같다 '유령'(또는 거짓, 그것을 따라 다름)라는 부울 필드를 기준으로 필터링 할.

번역 :

SELECT * 사용자로부터 WHERE 고스트 = 0;

cq.where()를 사용해야합니까? 방법?

답변

6

예, cq.where()을 사용해야합니다. 이 같은

시도 뭔가 :

Root<Utente> utente = cq.from(Utente.class); 
boolean myCondition = true; // or false 
Predicate predicate = cb.equal(utente.get(Utente_.ghost), myCondition); 
cq.where(predicate); 

내가 자동으로 생성해야 정식 메타 모델 클래스 Utente_을 사용했다. 이렇게하면 입력란 이름을 잘못 입력 할 위험이 없으며 형식 안전성이 향상됩니다. 그렇지 않으면 당신은 사용할 수 있습니다

Predicate predicate = cb.equal(utente.get("ghost"), myCondition); 
+0

괜찮아요하지만 cq.select (utente); –