Java JPA CriteriaBuilder OR Expression无法正常工作

时间:2014-09-19 20:17:58

标签: java jpa expression

这是我的方法:

CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<EmployersList> criteriaQuery = builder.createQuery(EmployersList.class);
Root<EmployersList> root = criteriaQuery.from(EmployersList.class);

Expression<String> orgPhoneNumberExp = root.get("orgphoneNumber");
Predicate orgPhoneNumberPredicate = orgPhoneNumberExp.in(phoneNumber);

Expression<String> accPhoneNumberExp = root.get("accphoneNumber");
Predicate accPhoneNumberPredicate = accPhoneNumberExp.in(phoneNumber);

Expression<String> orgStatusExp = root.get("orgphoneStatus");
Predicate orgStatusExpPredicate = builder.upper(orgStatusExp).in("SUSPECT", "FRAUD", "NEGATIVE");

Expression<String> accPhoneStatusExp = root.get("accphoneStatus");
Predicate accPhoneStatuPredicate = builder.upper(accPhoneStatusExp).in("SUSPECT", "FRAUD", "NEGATIVE");

Expression<Date> orgPhoneStatusExpDateExmp = root.get("orgphoneStatusExpdate");

Expression<Date> accPhoneStatusExpDateExmp = root.get("accphoneStatusExpdate");

Predicate orgPhoneStatusExpDatePredicate = builder.or(builder.greaterThanOrEqualTo(orgPhoneStatusExpDateExmp, new Date()), builder.isNull(orgPhoneStatusExpDateExmp));

Predicate accPhoneStatusExpDatePredicate = builder.or(builder.greaterThanOrEqualTo(accPhoneStatusExpDateExmp, new Date()), builder.isNull(accPhoneStatusExpDateExmp));

Expression<Boolean> orgExp = builder.and(orgPhoneNumberPredicate, builder.and(orgStatusExpPredicate, orgPhoneStatusExpDatePredicate)).as(Boolean.class);

Expression<Boolean> accExp = builder.and(accPhoneNumberPredicate, builder.and(accPhoneStatuPredicate, accPhoneStatusExpDatePredicate)).as(Boolean.class);

criteriaQuery.where(builder.or(orgExp, accExp));

List<EmployersList> result = entityManager.createQuery(criteriaQuery).getResultList();

所以,我需要在DB中检查'org'手机和'acc'手机。我需要检查...(如果orgphoneNumber IN(我将在方法参数中获得的电话号码列表)和orgStatusExp - SUSPECT或FRAUD或NEGATIVE和orgPhoneStatusExpDateExmp&gt; = curdate或null)否则...我需要检查相同'acc'手机的东西

使用此代码我得到选择WHERE:

where
    (
        employersl0_.ORGPHONE_NUMBER in (
            ?
        )
    ) 
    and (
        upper(employersl0_.ORGPHONE_STATUS) in (
            ? , ? , ?
        )
    ) 
    and (
        employersl0_.ORGPHONE_STATUS_EXPDATE>=? 
        or employersl0_.ORGPHONE_STATUS_EXPDATE is null
    ) 
    or (
        employersl0_.ACCPHONE_NUMBER in (
            ?
        )
    ) 
    and (
        upper(employersl0_.ACCPHONE_STATUS) in (
            ? , ? , ?
        )
    ) 
    and (
        employersl0_.ACCPHONE_STATUS_EXPDATE>=? 
        or employersl0_.ACCPHONE_STATUS_EXPDATE is null
    )

但我想得到这样的东西:

    where
    (
        employersl0_.ORGPHONE_NUMBER in (
            ?
        )
        and (
                upper(employersl0_.ORGPHONE_STATUS) in (
                ? , ? , ?
                )
            ) 
        and (
            employersl0_.ORGPHONE_STATUS_EXPDATE>=? 
            or employersl0_.ORGPHONE_STATUS_EXPDATE is null
        ) 
    ) 

    or (
        employersl0_.ACCPHONE_NUMBER in (
            ?
        )
        and (
            upper(employersl0_.ACCPHONE_STATUS) in (
                ? , ? , ?
            )
        ) 
        and (
            employersl0_.ACCPHONE_STATUS_EXPDATE>=? 
            or employersl0_.ACCPHONE_STATUS_EXPDATE is null
        )
    )

你能帮我解决这个问题吗?:)

1 个答案:

答案 0 :(得分:0)

对于这种情况,你最常定义3谓词  pridicate a do和on ORGPHONE_NUMBER,ORGPHONE_STATUS和ORGPHONE_STATUS_EXPDATE。
 pridicate b do and on ACCPHONE_NUMBER,ACCPHONE_STATUS和ACCPHONE_STATUS_EXPDATE。
 pridicate c do或on pridcate a和b。