我需要在查询中放入where子句,但语法不正确。

       /*
        * Support listing and POSTing back Origem entities (e.g. from inside an
        * HtmlSelectOneMenu)
        */

         public List<Origem> getAll()
   {

      CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
        CriteriaQuery<Origem> criteria = cb.createQuery(Origem.class);
        Root<Origem> root = criteria.from(Origem.class);
        return this.entityManager.createQuery(
                criteria.select(root).where())
                .getResultList();
   }


在我的where子句中,要带所有ID等于已登录用户的“原始”。
像这样:

例如:select * from origin where origem.id = loginBean.origem.getId

最佳答案

只需在您的条件中使用criteria.add(Restrictions.eq()),我认为如果id是唯一的,则需要使用.uniqueResult()从条件中获取所需结果,您的代码应如下所示:

CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
Criteria cr = cb.createCriteria(Origem.class);
// add the restriction here
cr.add(Restrictions.eq("id", loginBean.origem.getId));
Origem root = (Origem) cr.uniqueResult();

08-06 17:04