使用条件语句 - 条件API

时间:2018-03-23 14:40:11

标签: java jpa criteria-api

我的情况是,当我从 Primefaces selectCheckboxMenu 中选择元素时,我选择的元素之间应该有 OR 。如果我从数据表中过滤了另一列,那么在我从selectCheckboxMenu和另一列中选择的内容之间应该 AND

数据表 Datatable

从复选框中选择 Selection from checkbox

并过滤其他列 filter another column

代码

List<Predicate> predicates = new ArrayList<Predicate>();
Predicate[] predArray = new Predicate[predicates.size()];
Predicate finalQuery;

        columnName.forEach(name -> {
            if (name.equals("city")) {
                finalQuery = cb.or(predicates.toArray(predArray));
                predicates.add(finalQuery);
                cq.where(finalQuery);
            } else {
                predicates.toArray(predArray);
                cq.where(predArray);
            }
        });

如果我先选择某个列然后选择复选框,我的方案可以正常工作,但是当我第一次选择复选框然后再选择另一列时,我就失败了。

这是调试的SQL;

select user1_.id as col_0_0_, user1_.login as col_1_0_, user1_.first_name as col_2_0_, user1_.last_name as col_3_0_, user1_.day_of_birth as col_4_0_, userdto0_.district as col_5_0_, userdto0_.city as col_6_0_ from userdto userdto0_ inner join user user1_ on userdto0_.id=user1_.id where (lower(userdto0_.city) like ?) and (lower(userdto0_.city) like ?) and (lower(TO_CHAR(cast(user1_.id as varchar(255)))) like ?) order by user1_.id asc

很抱歉,如果这个问题很愚蠢,但我对这些问题不熟悉。

0 个答案:

没有答案