I need to get data from the database. There are two variables: LocalDate startDate and LocalDate endDate. Request can be:
startDate-null- you need to select all entries from startDate to the current date.
null-endDate-you need to select all records to endDate.
startDate-endDate - select all entries from startDate to endDate.
null-null- select all entries.
Database search method:
CriteriaBuilder cb = manager.getCriteriaBuilder(); CriteriaQuery<Supply> query = cb.createQuery(Supply.class); Root<Supply> supplyRoot = query.from(manager.getMetamodel().entity(Supply.class)); List<Predicate> predicates = new ArrayList<Predicate>(); if(startDate!=null && endDate!=null){ Predicate betweenPredicate = cb.between(supplyRoot.<LocalDate>get("arrivalDate"), startDate, endDate); predicates.add(betweenPredicate); }else if(startDate!=null && endDate == null){ Predicate greaterThanPredicate = cb.greaterThanOrEqualTo(supplyRoot.<LocalDate>get("arrivalDate"), startDate); predicates.add(greaterThanPredicate); } else if(startDate == null && endDate!= null){ Predicate lessThanPredicate = cb.lessThanOrEqualTo(supplyRoot.<LocalDate>get("arrivalDate"), endDate); predicates.add(lessThanPredicate); } if(!predicates.isEmpty()){ query.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); query.select(supplyRoot); } return manager.createQuery(query).getResultList(); The problem is that when searching between two dates, only the first value from the database is returned. For example, the database has a date 12/25/2016. If you make a request for a choice from 12/24/2016 to 12/26/2016 then it does not find anything.
MySQL database, column type date.
Where can there be a problem?