Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-data @Query mapping result issue

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;
}
like image 885
Jonas Geiregat Avatar asked Oct 08 '22 02:10

Jonas Geiregat


1 Answers

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];

  }
like image 51
Phani Avatar answered Oct 13 '22 10:10

Phani