Hashmap type attribute appears as Object

Asked

Viewed 95 times

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();

It was created the Service that implements this method of the Repository interface in a function and soon after the Controller instance that Service and calls the method responsible for rescuing the values of the database from the service method that then calls this function of the repository.

But I hoped that in the attribute of the type List an Array with type attributes appears HashMap but the attributes appear to be of the type Object and when I traverse the attributes with a foreach for example or even with a for crash an error saying that it was not possible to cast Object for HashMap.

inserir a descrição da imagem aqui

Any solution to this problem?

2 answers

1


A Typedquery would not be a possible solution for your case? Since you would create a class that would simulate the behavior of Hashmap.

  • 1

    Thanks, I didn’t know about the TypedQuery and your answer leads to links as to the solution I found.

  • Typedquery is one of the resources that help in our development life. I am happy to have helped, even if indirectly.

0

Problem solved, the Interface method has been changed staying this way:

@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();

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.