简体   繁体   中英

Java JPA CriteriaBuilder OR Expression does not work correctly

This is my method:

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();

So, i need check 'org' Phones and 'acc' Phones in DB. I need check...(if orgphoneNumber IN(list of phone numbers which i will get in method parametr) and if orgStatusExp - SUSPECT or FRAUD or NEGATIVE and orgPhoneStatusExpDateExmp >= curdate or null) otherwise ... i need to check same things for 'acc' Phones

with this code i get Select with 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
    )

but i want to get something like this:

    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
        )
    )

Can you help me with this issue?:)

For this case you most define 3 Predicate.
pridicate a do and on ORGPHONE_NUMBER, ORGPHONE_STATUS and ORGPHONE_STATUS_EXPDATE.
pridicate b do and on ACCPHONE_NUMBER, ACCPHONE_STATUS and ACCPHONE_STATUS_EXPDATE.
pridicate c do or on pridcate a and b.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM