Criteria Builder Create new Object In Select Statement

后端 未结 1 1392
遥遥无期
遥遥无期 2021-02-01 06:25

I was wondering if it\'s possible to create such query like :

em.createQuery(
        \"SELECT NEW EmpMenu(p.name, p.department.name) \"
            + \"FROM Pro         


        
1条回答
  •  执笔经年
    2021-02-01 07:03

    Yes, Criteria API does have have construct similar to JPQL constructor expressions. Result class is set via construct method in CriteriaBuilder.

    Your JPQL query expressed as an criteria query is:

    CriteriaBuilder cb...
    CriteriaQuery q = cb.createQuery(EmpMenu.class);
      Root c = q.from(Project.class);
      q.select(cb.construct(EmpMenu.class,
          c.get("name"), c.get("department").get("name")));
    

    0 讨论(0)
提交回复
热议问题