0

I need to pick all pending bookings from next hour till next two hours. I have criteria like this:

calendar.setTime(new Date());
    calendar.add(Calendar.HOUR, 1);
    cb.and(cb.greaterThanOrEqualTo(bookingRoot.<Date>get("pickupTime"), calendar.getTime()));
    calendar.add(Calendar.HOUR, 2);
    cq.where(cb.and(cb.equal(bookingRoot.get("status"), CabStatus.PENDING), cb.lessThanOrEqualTo(bookingRoot.<Date>get("pickupTime"), calendar.getTime())));
    return getEntityManager().createQuery(cq).getResultList();

But it picks all bookings having pickup time less than than the time after 2 hours. Any help appreciated.

1 Answer 1

1

Your first cb.and(...) is not in the where- part. You should make something like that:

calendar.setTime(new Date());
calendar.add(Calendar.HOUR, 1);
Predicate greater = cb.and(cb.greaterThanOrEqualTo(bookingRoot.<Date>get("pickupTime"),calendar.getTime()));
calendar.add(Calendar.HOUR, 2);
Predicat lower = cb.and(cb.equal(bookingRoot.get("status"), CabStatus.PENDING), cb.lessThanOrEqualTo(bookingRoot.<Date>get("pickupTime"), calendar.getTime())));
cq.where(cb.and(greater,lower);
return getEntityManager().createQuery(cq).getResultList();

You can use system.out.println to see the query that JPA create:

 TypedQuery<ENTITYTYPE> typedQuery = em.createQuery(cq);
 System.out.println(typedQuery);
    return typedQuery.getResultList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.