Criteriabuilder

Asked

Viewed 166 times

0

Good evening. Please, I would like to ask you for help for the code below. It returns the total value (total values, saved in the database), but I need to return by month. I tried everything I knew, but I couldn’t solve the problem. I thank you in advance for the help.

EntityManagerFactory emf = Persistence.createEntityManagerFactory("belezaPU");
    EntityManager em = emf.createEntityManager();

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<BigDecimal> criteriaQuery = builder.createQuery(BigDecimal.class);

    Root<Venda> venda = criteriaQuery.from(Venda.class);
    criteriaQuery.select(builder.sum(venda.<BigDecimal> get("precoTotal")));

    TypedQuery<BigDecimal> query = em.createQuery(criteriaQuery);
    BigDecimal totalVendas = query.getSingleResult();

    System.out.println("A soma das vendas é: R$" + totalVendas);
  • What is the field name that contains the date of sale?

  • Hello Augusto, I’m sorry for the delay. The field is called schedule.

1 answer

0

Use the method between of the interface CriteriaBuildertesting the first expression to see if it is between the second and third expression.

In the example I use your code to sum sales in April. Only significant change in your code is the highlighted snippet:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("belezaPU");
EntityManager em = emf.createEntityManager();

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<BigDecimal> criteriaQuery = builder.createQuery(BigDecimal.class);

Root<Venda> venda = criteriaQuery.from(Venda.class);


// ***********************************************************************
Date dataInicial = new SimpleDateFormat("dd/MM/yyyy").parse("01/04/2019");
Date dataFinal= new Date();
criteriaQuery.select(builder.sum(venda.<BigDecimal>get("precoTotal"))).where(builder.between(venda.<Date>get("horario"), dataInicial, dataFinal)); 
// ***********************************************************************

TypedQuery<BigDecimal> query = em.createQuery(criteriaQuery);
BigDecimal totalVendas = query.getSingleResult();

System.out.println("A soma das vendas em abril é: R$" + totalVendas);
  • 1

    Thank you very much Augusto. I will tetstar right now.

  • @Rubenscostadasilva. I had a mistake. I edited.

  • Good afternoon Augusto. I re-did, or better added the lines you gave me. I created the date filter file. Everything was OK, except for small detail as below: is giving an error that I could not understand that is the get: criteriaQuery.select(Builder.sum(sale. <Bigdecimal>get("precoTotal"))) . Where(Builder.between(order. <Date>get("horario"), dataInitial, dataFinal));

  • The error in "get" has already been fixed. I will now test to see if it returns the values per month.

  • 1

    Bravo! It worked. Thank you very much dear Augusto. Now I can continue with my project thanks to your invaluable help. Thank you very much.

Browser other questions tagged

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