Help about a JPA query

0

I'm trying to make a query using JPA criteria, it would look like this:

I'll go through 2 initial dates and a DateFinal and a vehicle's board how can I make this query?

The table that I'm going to consult would be Service and it has a car where this car would have a license plate, I think that up to that point is correct.

public List<Servico> buscarServicoDeVeiculoEntreDatas(Date diaServico, Date diaGarantia, Carro carro) {

        CriteriaBuilder builder = manager.getCriteriaBuilder();
        CriteriaQuery<Servico> criteriaQuery = builder.createQuery(Servico.class);

        Root<Servico> s = criteriaQuery.from(Servico.class);
        criteriaQuery.select(s);
    
asked by anonymous 08.04.2017 / 03:33

1 answer

0
        ParameterExpression<Date> dataInicial = builder.parameter(Date.class, "dataInicial");
        ParameterExpression<Date> dataFinal = builder.parameter(Date.class, "dataFinal");

    criteriaQuery.where(builder.between(s.get("data"), dataInicial, dataFinal));

    TypedQuery<Servico> query = manager.createQuery(criteriaQuery);

        Calendar dataIni = Calendar.getInstance();
        dataIni.setTime(Seta sua data aqui);
        dataIni.set(Calendar.HOUR_OF_DAY, 0);
        dataIni.set(Calendar.MINUTE, 0);
        dataIni.set(Calendar.SECOND, 0);

        Calendar dataFin = Calendar.getInstance();
        dataFin.setTime(Seta sua data aqui);
        dataFin.set(Calendar.HOUR_OF_DAY, 23);
        dataFin.set(Calendar.MINUTE, 59);
        dataFin.set(Calendar.SECOND, 59);

        query.setParameter("dataInicial", dataIni.getTime());
        query.setParameter("dataFinal", dataFin.getTime());


    return query.getResultList();

Add this snippet to the end and adapt to your project.

    
08.04.2017 / 17:28