代码之家  ›  专栏  ›  技术社区  ›  jbandi

在列表属性上使用不带元模型的JPA2标准API

  •  14
  • jbandi  · 技术社区  · 15 年前

    如何在不使用元模型类的情况下制定以下JPA2条件查询:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
    Root<Employee> emp = cq.from(Employee.class);
    cq.where(cb.isEmpty(emp.get(Employee_.projects)));
    cq.select(emp);
    

    我想使用:

    cq.where(cb.isEmpty(emp.get("projects")));
    

    谢谢。

    1 回复  |  直到 5 年前
        1
  •  21
  •   Pascal Thivent    14 年前

    试试这个:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(Employee.class);
    Root emp = cq.from(Employee.class);
    cq.where(cb.isEmpty(emp.<List<Project>>get("projects")));
    cq.select(emp);

    或者,使用 Path 变量:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(Employee.class);
    Root emp = cq.from(Employee.class);
    Path<List<Project>> projects = emp.get("projects"));
    cq.where(cb.isEmpty(projects);
    cq.select(emp);