Spring -data @Query displaying the result

I created a repository that extends CrudRepository, this repository has a method with @Query notation:

the 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 problem I am facing is that this returns an ArrayList of the object (s), not a list of maps. I read somewhere that JPA cannot return the map, so I find the result in List>.

I do not know how best to get around this problem or quickly access the result data. I tried casting, but that didn't work either:

for(Object item: items) {
    Map<String,Long> castedItem = (HashMap<String,Long>)item;
}
+5
source share
1 answer

See this example in the official Hibernate documentation. Here

 for (Object item:items) {
   Object[] tuple = (Object[]) item;
    String itemType = (String)tuple[0];
    Long count = (Long) tuple[1];

  }
+4
source

All Articles