javaspring-bootspring-data-jpacriteria-apicriteriaquery

How to apply a criteria builder together with an arraylist in JPA?


I am trying to receive an array with city names in my api and create a filter with criteria bulder that returns that list of cities.

This is what I have in my controller

@GetMapping("/weather")
    public @ResponseBody List<Weather> weatherGetALL(
            @RequestParam(required = false) String order,
            @RequestParam(required = false) List<String>  city,
            @DateTimeFormat(pattern = DATE_PATTERN) Date date) {
            return this.weatherService.findAll(order, city, date);
    }

And this is what I have in my service to apply these types of filter

@Autowired
WeatherRepository _weatherRepo;

public Weather save(Weather weather) {
    return this._weatherRepo.save(weather);
}

public Weather findeById(Integer id) {
    return this._weatherRepo.findById(id).orElse(null);
}


public List<Weather> findAll(String order, List<String>  city, Date date  ) {

    List<Weather> weather  = this._weatherRepo.findAll((Specification<Weather>)(root, cq, cb) -> {
        Predicate p = cb.conjunction();

        if(!(city == null)){
            System.out.println("entra en el arreglo");  

//          for (String name : city) {
//              p = cb.and(p, cb.equal(root.get("city"), name));
//          }


        }

        /*if (!StringUtils.isEmpty(city)) {
        System.out.println("ciudad:" + name);
            p = cb.and(p, cb.equal(root.get("city"), city ));
        }*/

        if (Objects.nonNull(date)) {
            p = cb.and(p, cb.equal(root.get("date"), date ));
        }


        if (!StringUtils.isEmpty(order)) {
            if (!StringUtils.isEmpty(order)) {
                switch(order) {
                    case "date":
                        cq.orderBy(cb.asc(root.get("date")));
                        break;
                    case "-date":
                        cq.orderBy(cb.desc(root.get("date")));
                        break;
                    default:
                        cq.orderBy(cb.asc(root.get("id")));
                }
            }
        }



        return p;
    });

    return weather;

}

I read that with the "in" operator I can do it but I have not found the correct way to apply it. Apparently I receive everything fine, the only thing that fails me is the filtering of that part


Solution

  • Try this

    public List<Weather> findAll(String order, List<String> city, Date date) {
       List<Weather> weather  = this._weatherRepo.findAll((Specification<Weather>) (root, cq, cb) -> {
                final List<Predicate> predicates = new ArrayList<>();
    
                if (date != null) {
                    predicates.add(cb.equal(root.get("date"), date));
                }
    
                if (city != null && !city.isEmpty()) {
                    predicates.add(root.get("city").in(city));
                }
    
                if (order != null && order.equals("-date")) {
                    cq.orderBy(cb.desc(root.get("date")));
                } else {
                    cq.orderBy(cb.asc(root.get("date")));
                }
    
    //            return cb.or(predicates.toArray(new Predicate[]{}));
                return cb.and(predicates.toArray(new Predicate[]{}));
            });
      
       return weather;
    }