TypedQuery instead of normal Query in JPA

前端 未结 2 2057
遇见更好的自我
遇见更好的自我 2021-02-08 03:18

Is it possible to write this Query as a TypedQuery and let the two Long\'s run into a Object with two public Long fields inside.

    Query q = em.createQuery(
           


        
2条回答
  •  再見小時候
    2021-02-08 03:53

    JPA has a feature just for this - constructor expressions:

    Query q = entityManager.createQuery("SELECT NEW com.example.DTO( c.id, COUNT(t.id)) FROM ...");
    List dtos = q.getResultList();
    

    Your DTO class can be a POJO. All it will need is a public constructor accepting 2 Longs. Please note that you have to provide a fully qualified name of your class after the NEWoperator.

提交回复
热议问题