I have some method that return an object and a count, due to the following SQL request
public List<Sales> mostLoyal() {
String queryString = "SELECT s, COUNT(s.saleId) as TotalNum FROM Sales s GROUP BY s.custId.custLastName ORDER BY TotalNum DESC";
List<Sales> breakdown = em.createQuery(queryString).setMaxResults(10).getResultList();
return breakdown;
}
I'm trying to transform this List into a Hashmap containing the object (s in the request) and the count. I've tried a lot of things but I can't figure for the life of me out how to do that.
The goal is to return this Hashmap into a json.
You can simply iterate through your list and place values in HashMap and return it. Should be simple.
public Map<String,Integer> mostLoyal() {
Map<String,Integer> m = new HashMap<String,Integer>();
String queryString = "SELECT s, COUNT(s.saleId) as TotalNum FROM Sales s GROUP BY s.custId.custLastName ORDER BY TotalNum DESC";
List<Sales> breakdown = em.createQuery(queryString).setMaxResults(10).getResultList();
for(Sales sale: breakdown){
m.put(sale.getS(),sale.getCount());
}
return m;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.