“或”谓词内部“和”谓词使用JPQL

时间:2017-11-28 02:54:59

标签: java sql jpql

我想使用JPQL进行选择查询,但我的代码有误, 这是代码

private CriteriaQuery<TbCase> getQueryParams(TbCaseSearchParams params, CriteriaBuilder cb, CriteriaQuery<TbCase> cq,Root<TbCase> root){

    List<Predicate> criteria = new ArrayList<Predicate>();

    if(params.getEqColumns() != null && params.getEqValues() != null){
        criteria = getEqParamsCriteria(criteria, cb, root, params.getEqColumns(), params.getEqValues());
    }else if(params.getLikeColumns() != null && params.getLikeValues() != null){
        criteria = getLikeParamsCriteria(criteria, cb, root, params.getLikeColumns(), params.getLikeValues());
    }else{  

        if(params.getCaseNumber() != null){
            criteria.add(cb.equal(root.get("caseNumber"), params.getCaseNumber()));
        }

        if(params.getCaseStatusIds() != null){
            List<String> caseStatusIds = new ArrayList<String>();
            caseStatusIds = params.getCaseStatusIds();
            for(int i=0 ; i<caseStatusIds.size() ; i++){
                criteria.add(cb.or(cb.equal(root.get("caseStatusByCaseStatusId"), Integer.valueOf(params.getCaseStatusIds().get(i)))));
            }
        }
    }

    cq.select(root).where(cb.and(criteria.toArray(new Predicate[0])));

    return cq;
}

这是SQL表单中的查询结果

select something from TABLENAME as something where ( something.caseNumber=:param0 ) and ( something.caseStatusByCaseStatusId=1 ) and ( something.caseStatusByCaseStatusId=-1 ) and ( something.caseStatusByCaseStatusId=1 ) and ( something.caseStatusByCaseStatusId=9 ) and ( something.caseStatusByCaseStatusId=4 ) and ( something.caseStatusByCaseStatusId=5 ) and ( something.caseStatusByCaseStatusId=10 ) and ( something.caseStatusByCaseStatusId=2 ) order by something.caseNumber desc

,预期的查询就像这样

select something from TABLENAME as something where ( something.caseNumber=:param0 ) and ( something.caseStatusByCaseStatusId=1 or something.caseStatusByCaseStatusId=-1 or something.caseStatusByCaseStatusId=1 or something.caseStatusByCaseStatusId=9 or something.caseStatusByCaseStatusId=4 or something.caseStatusByCaseStatusId=5 or something.caseStatusByCaseStatusId=10 or something.caseStatusByCaseStatusId=2 ) order by something.someCriteria desc

如何处理我的代码以获得预期的查询结果?如果您需要其他详细信息,我会立即更新thx。

1 个答案:

答案 0 :(得分:0)

我自己没有测试过以下代码段。但它应该是如何构建标准查询的方式:

 Predicate andCriteria = cb.conjunction();

if (params.getEqColumns() != null && params.getEqValues() != null) {
    andCriteria = cb.and(andCriteria, cb.equal(...);
    // ... furhter andCriteria
} else if (params.getLikeColumns() != null && params.getLikeValues() != null) {
    andCriteria = cb.and(andCriteria, cb.like(...);
    // ... furhter andCriteria
} else {  
    if(params.getCaseNumber() != null){
        andCriteria = cb.and(andCriteria, cb.equal(root.get("caseNumber"), params.getCaseNumber()));
    }
    if (params.getCaseStatusIds() != null) {
        List<String> caseStatusIds = new ArrayList<String>();
        caseStatusIds = params.getCaseStatusIds();
        Predicate orCriteria = cb.disjunction();
        for (int i=0 ; i < caseStatusIds.size() ; i++) {
            orCriteria = cb.or(orCriteria, cb.equal(root.get("caseStatusByCaseStatusId"), Integer.valueOf(params.getCaseStatusIds().get(i))));
        }
        andCriteria = cb.and(andCriteria, orCriteria);
    }
}

cq.select(root).where(andCriteria);

请注意,您必须调整getEqParamsCriteria()getLikeParamsCriteria()中的代码。