TypedQuery instead of normal Query in JPA

前端 未结 2 2043
遇见更好的自我
遇见更好的自我 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:41

    New code looks like this now. Thanks for you help.

        TypedQuery q = em.createQuery(
            "SELECT new CommUsed(c.id,COUNT(t.id)) " +
            "FROM PubText t " +
            "JOIN t.comm c " +
            "WHERE c.element = ?1 " +
            "GROUP BY c.id", CommUsed.class);
        q.setParameter(1, e);
        HashMap res = new HashMap();
        for (CommUsed u : q.getResultList())
            res.put(u.commID, u.cnt);
    

提交回复
热议问题