Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单JPA 2标准查询“;其中;条件_Jpa_Jpa 2.0_Criteria - Fatal编程技术网

简单JPA 2标准查询“;其中;条件

简单JPA 2标准查询“;其中;条件,jpa,jpa-2.0,criteria,Jpa,Jpa 2.0,Criteria,我正在学习jpa hibernate的基础知识 我有此查询用于获取所有用户: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); cq.select(cq.from(Utente.class)); return getEntityManager().createQuery(cq).getResultLis

我正在学习jpa hibernate的基础知识

我有此查询用于获取所有用户:

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        cq.select(cq.from(Utente.class));
        return getEntityManager().createQuery(cq).getResultList();
现在我想通过一个名为“ghost”的布尔字段进行过滤,其中它等于true(或者false,这取决于它)

翻译成:

从ghost=0的用户中选择*


我必须使用cq.where()吗?如何使用?

是的,您必须使用
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);
Predicate predicate = cb.equal(utente.get("ghost"), myCondition);