org.hibernate.Propertynotfoundexception: Could not find Setter for sum on class Class

Asked

Viewed 458 times

3

People I’m trying to list the best selling products using Sqlquery (if anyone knows of other ways to perform as eg using Criteria, Projections also accepted) with Hibernate, but this error is resulting:

    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();
  • Friend, I put an answer but rereading your code I have some doubts. produto_id is the sequence of ItemPedidoVenda? You intend to return a result of ItemPedidoVenda? Why for me at the moment, it doesn’t make sense for you to ask for the select return the quantity sum and the product id, while you want to return the whole object.

  • 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.

  • Reply edited @Alyssonoliveira, please see if it helps you.

1 answer

1

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

Browser other questions tagged

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