How to return HashMap from JPA query?

前端 未结 3 1133
时光取名叫无心
时光取名叫无心 2020-12-30 11:58

I want to return a HashMap from JPA query like the below but I don\'t know how to fill the HashMap from this query. Actually I want to fill charts from HashMap in the fronte

3条回答
  •  半阙折子戏
    2020-12-30 12:45

    Please refer, JPA 2.0 native query results as map

    In your case in Postgres, it would be something like,

    List list = em.createNativeQuery("select cast(json_object_agg(count(i.uuid),i.username) as text) from schema.information i where i.entereddt between :start and :end group by i.username")
                       .setParameter("start",new Timestamp(start.getTime()))
                       .setParameter("end",new Timestamp(end.getTime()))
                       .getResultList();
    
    //handle exception here, this is just sample
    Map map = new ObjectMapper().readValue(list.get(0), Map.class);
    

    Kindly note, I am just sharing my workaround with Postgres.

提交回复
热议问题