Attribute of type HashMap appears as Object

0

I'm using the Spring framework and the repositories, and in one of the interfaces of one of these repositories I have, for example, a method like this:

@Query("select extract(month from u.atributo1), coalesce(sum(u.atributo2), 0) from #{#entityName} u where extract(year from u.atributo1) = extract(year from current_date()) group by extract(month from u.atributo1)")
public List<HashMap<Integer, BigDecimal>> getDoisAtributos();
The Service that implements this method of the Repository interface on a role has been created and then the Controller instantiates that Service and calls the method responsible for retrieving the database values from the service method that then calls that function repository.

But I expected that in the attribute of type List there would appear an Array with the attributes of type HashMap but it appears the attributes being type Object and when I run the attributes with a foreach for example or even with a for pops up an error saying that it was not possible to cast Object to HashMap .

Any solutions to this problem?

    
asked by anonymous 21.10.2015 / 15:10

2 answers

1

Would not a TypedQuery be a possible solution to your case? Since you would create a class that would simulate the behavior of HashMap.

    
21.10.2015 / 15:33
0

Problem solved, the Interface method has been changed by going like this:

@Query("select new Map(extract(month from u.atributo1), coalesce(sum(u.atributo2), 0)) from #{#entityName} u where extract(year from u.atributo1) = extract(year from current_date()) group by extract(month from u.atributo1)")
public List<HashMap<Integer, BigDecimal>> getDoisAtributos();

    
21.10.2015 / 15:45