org.hibernate.PropertyNotFoundException: Could not find setter for sum on class Class

1

I'm trying to list the best selling products using SQLQuery (if anyone knows of other ways to perform such as eg using Criteria, Projections also supported) with Hibernate,

    org.hibernate.PropertyNotFoundException: Could not find setter for sum on class pojo.ItemPedidoVenda

The code:

SQLQuery query = session.createSQLQuery("select sum(quantidade), produto_id from itempedidovenda group by produto_id order by sum(quantidade) desc");
    query.setResultTransformer(Transformers.aliasToBean(ItemPedidoVenda.class));
    List<ItemPedidoVenda> items = query.list();

    return query.list();
    
asked by anonymous 06.09.2016 / 20:18

1 answer

0

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
    )
)
    
06.09.2016 / 20:24