Condition left join in CriteriaQuery

无人久伴 提交于 2019-12-05 09:30:51

The on clause has been introduced in JPA 2.1. An example solution:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> cq = cb.createQuery(String.class);
    Root<Empl> a = cq.from(Empl.class);
    Join<Empl,Deplo> b = a.join("b", JoinType.LEFT); //left outer join
    b.on(
        cb.and(
            cb.equal(b.get("id_place"), cb.parameter(Integer.class, "idPlace")),
            cb.equal(b.get("id_depl"), a.get("id_depl"))
        )   
    );
    cq.select(b.<String>get("name"));

    List<String> results = em.createQuery(cq)
                             .setParameter("idPlace", 2)
                             .getResultList();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!