spring-data @Query mapping result issue
java, spring, spring-data
Solution
See this example in official documentation of Hibernate.Here
for (Object item:items) {
Object[] tuple = (Object[]) item;
String itemType = (String)tuple[0];
Long count = (Long) tuple[1];
}
Problem
I've created a Repository that extends CrudRepository, this repository has a method with an @Query notation: Code: ``` @Query("select itemType, count(*) as count from Item where User_id = :userId group by itemType") List<Map<String, Long>> countItemsForUser(@Param("userId") Long userId); ``` The issue I'm having is that this return a ArrayList of Object(s) and not a List of Map. I've read somewhere that JPA can't return a Map so that's why I stuff the result in a List>. I don't know what's the best way to work around this issue or to quickly access the result data. I've tried casting but that didn't work out either: ``` for(Object item: items) { Map<String,Long> castedItem = (HashMap<String,Long>)item; } ```