6

I need to create a dynamic query. I tried to do it using JPQL, but could not. For example:

    public List get(String category, String name, Integer priceMin, Integer priceMax){
    List<Prod> list;
    String query = "select p from Prod p where 1<2 ";
    String queryCat="";
    String queryName="";
    String queryPriceMin="";
    String queryPriceMax="";
    String and = " and ";
    if (!category.isEmpty()){
        query+=and+"p.cat.name=:category ";
    }
    if (!name.isEmpty()){
        query+=and+"p.name=:name ";
    }
    if (priceMin!=null){
        query+=and+"p.price>=:priceMin ";

    }
    if (priceMax!=null){
        query+=and+"p.price<=:priceMax ";
    }
    return list = entityManager.createQuery(query)
            .setParameter("category", category)
            .setParameter("name",name)
            .setParameter("priceMin", priceMin)
            .setParameter("priceMax", priceMax)
            .getResultList();

}

If there are all the parameters, the query runs, but if there is no such parameter category I have Exception java.lang.IllegalArgumentException: Parameter with that name [category] did not exist and I understand why is it so, but how I can avoid this problem?

ooozguuur
  • 3,396
  • 2
  • 24
  • 42
Kadzhaev Marat
  • 1,278
  • 1
  • 19
  • 33

2 Answers2

20

You can try.

    public List<Prod> get(String category, String name, Integer priceMin, Integer priceMax){
    Map<String, Object> paramaterMap = new HashMap<String, Object>();
    List<String> whereCause = new ArrayList<String>();

    StringBuilder queryBuilder = new StringBuilder();
    queryBuilder.append("select p from Prod p ");

    if (!category.isEmpty()){
        whereCause.add(" p.cat.name =:category ");
        paramaterMap.put("category", category);
    }
    if (!name.isEmpty()){
        whereCause.add(" p.name =:name ");
        paramaterMap.put("name", name);
    }
    if (priceMin!=null){
        whereCause.add(" p.price>=:priceMin ");
        paramaterMap.put("priceMin", priceMin);
    }
    if (priceMax!=null){
        whereCause.add("p.price<=:priceMax  ");
        paramaterMap.put("priceMax", priceMax);
    }

    //.................
    queryBuilder.append(" where " + StringUtils.join(whereCause, " and "));
    Query jpaQuery = entityManager.createQuery(queryBuilder.toString());

    for(String key :paramaterMap.keySet()) {
            jpaQuery.setParameter(key, paramaterMap.get(key));
    }

    return  jpaQuery.getResultList();

}
ooozguuur
  • 3,396
  • 2
  • 24
  • 42
0

You can use JpaSpecificationExecutor I will explain it with my example.

  • In the Repository

     public interface ABCRepository extends JpaRepository<ABC, UUID> , JpaSpecificationExecutor<Event> {
    

    }

  • In a Service method you can use it as follows


public List search(EventSearch eventSearch) {

        return eventRepository.findAll(new Specification<Event>(){
            @Override
            public Predicate toPredicate(Root<Event> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!eventSearch.getTitle().equals(null)) {
                    predicates.add(criteriaBuilder.like(root.get("title"), "%" + eventSearch.getTitle() + "%"));
                }

            if(!eventSearch.getLocation().equals(null)) {
                predicates.add(criteriaBuilder.like(root.get("location"), "%" + eventSearch.getLocation() + "%"));
            }

            if(!eventSearch.getStartDate().equals(null)){
                predicates.add(criteriaBuilder.greaterThan(root.get("startDate"), eventSearch.getStartDate()));
            }

            if(!eventSearch.getEndDate().equals(null)){
                predicates.add(criteriaBuilder.greaterThan(root.get("endDate"), eventSearch.getEndDate()));
            }

            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    });
}