Using Hibernate Query Language (HQL) (or JPQL, cannot differentiate them), you induce the compiler to believe that there is an attribute sum
inside ItemPedidoVenda
.
To solve you could create even the attribute and put your getters//setters
(for that is what the error is about, is not found getSum
and setSum
) or use native SQL and recover from an array of Object
.
I’ll leave the description above because that’s what’s wrong anyway.
What you should realize is that the HQL select sum(quantidade), produto_id from ...
will return a Long
and the type of the variable that is produto_id
, then there’s no way to convert it to ItemPedidoVenda
. what you can do is run the following HQL SELECT MAX(SUM(ipv.quantidade)), ipv.produto_id FROM ItemPedidoVenda ipv
, withdraw the return ivp.produto
and use in a query in Itempedidovenda using object code (SELECT ipv FROM ItemPedidoVenda ipv WHERE ipv.produto_id = :produtoId
). In code 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
)
)
Friend, I put an answer but rereading your code I have some doubts.
produto_id
is the sequence ofItemPedidoVenda
? You intend to return a result ofItemPedidoVenda
? Why for me at the moment, it doesn’t make sense for you to ask for theselect
return the quantity sum and the product id, while you want to return the whole object.– Gustavo Cinque
In fact the product field_id is the product id in the itempedidovenda table, I would like to return the product that more "appears" in this table, using the above sql works (tested in SGBD), but I need to accomplish this using HIBERNATE. If you can help me right now I’d appreciate it.
– Alysson Oliveira
Reply edited @Alyssonoliveira, please see if it helps you.
– Gustavo Cinque