Using Hibernate Query Language ( HQL ) (or JPQL , I can not differentiate them), you induce the compiler to believe that there is a sum
within ItemPedidoVenda
.
To solve you could even create the attribute and put your getters//setters
(because that's what the error is about, getSum
and setSum
is not found) or use native SQL and retrieve from a vector of Object
.
I will leave the above description because it is what is wrong anyway.
What you should realize is that HQL select sum(quantidade), produto_id from ...
will return a Long
and the type of the variable that is produto_id
, so there is no way to convert it to ItemPedidoVenda
. what you can do is to execute the following% HQL%, remove the SELECT MAX(SUM(ipv.quantidade)), ipv.produto_id FROM ItemPedidoVenda ipv
return, and use in a query in SalesObject using the object code ( ivp.produto
). In code it would look something like this.
query = createQuery("SELECT MAX(SUM(ipv.quantidade)), ipv.produto_id
FROM ItemPedidoVenda ipv");
Object[] resultados = query.getResult();
query = createQuery("SELECT ipv FROM ItemPedidoVenda ipv WHERE
ipv.produto_id = :produtoId");
query.setParameter(resultados[1]); //Trate aqui caso não tenha nada dentro de resultados;
ItemPedidoVenda objetoEsperado = query.getSingleResult();
It would even work using a subselect within the second select:
SELECT ipv FROM ItemPedidoVenda ipv WHERE ipv.produto_id = (
SELECT produtoId FROM (
SELECT MAX(SUM(ipv.quantidade)), ipv.produto_id as produtoId
FROM ItemPedidoVenda ipv
)
)