Really dynamic JPA CriteriaBuilder

前端 未结 5 1575
盖世英雄少女心
盖世英雄少女心 2020-12-02 05:50

I need to create a \"real\" dynamic JPA CriteriaBuilder. I get an Map with the statements. It looks like:

nam         


        
5条回答
  •  离开以前
    2020-12-02 06:20

    One option is to use the fact that method with variable number of arguments can take an array:

    query.where(predicates.toArray(new Predicate[predicates.size()])); 
    

    Alternatively, you can combine them into a single predicate (note that if you don't do it, you don't need to create a conjunction as in your example);:

    Predicate where = cb.conjunction();
    while (column.hasNext()) {
        ...
        where = cb.and(where, cb.equal(userRoot.get(colIndex), colValue));
    }
    
    query.where(where);
    

提交回复
热议问题