How to return HashMap from JPA query?

前端 未结 3 1127
时光取名叫无心
时光取名叫无心 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条回答
  •  Happy的楠姐
    2020-12-30 12:34

    It appears that you were trying to execute a query which return types not mapped to any Java entities you have (or if they be present you never mentioned them). In this case, you want to use createNativeQuery(), which will return a List of type Object[].

    Try using this version of the method:

    public HashMap getCount(Date start,Date end) {
        HashMap map=new HashMap();
        Query q = em.createNativeQuery(
                        "select count(i.uuid),i.username from Information i" +
                        "where i.entereddt between :start and :end group by i.username");
        q.setParameter("start",new Timestamp(start.getTime()));
        q.setParameter("end",new Timestamp(end.getTime()));
    
        List list = query.getResultList();
    
        for (Object[] result : list) {
            map.put(result[0].toString(), result[1].toString());
        }
    
        return map;
    }
    

提交回复
热议问题