简体   繁体   中英

Spring data jpa "IN" clause using specification

i am trying to get the records from the database using spring data jpa Speicification API. here i need to put a condition for "In" clause for status column, for that i am code like below.

public static Specification<UserEntity> userSpecificationsforOperator(String orgName, String groupID,
            List<String> status, Date startDate, Date endDate) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (orgName != null) {
                Join<UserEntity, OrganizationEntity> organization = root.join("organization");
                predicates.add(builder.equal(organization.get("name"), orgName));
            }
            /*
             * if (groupID != null) {
             * predicates.add(builder.equal(root.get("refApprovalGroupId"), groupID)); }
             */

            if (status != null && status.size()>0) {
                predicates.add(root.get("status").in(status));
            }

            if (startDate != null) {
                predicates.add(builder.greaterThanOrEqualTo(root.get("createdDate"), startDate.toInstant()));
            }
            if (endDate != null) {
                predicates.add(builder.lessThanOrEqualTo(root.get("createdDate"), endDate.toInstant()));
            }
            Predicate[] p = predicates.toArray(new Predicate[predicates.size()]);
            return p.length == 0 ? null : p.length == 1 ? p[0] : builder.and(p);
        };
    }

the above code generating the query in cosole like below

SELECT userentity0_.id                    AS id1_68_,
       userentity0_.created_by            AS created_2_68_,
       userentity0_.created_date          AS created_3_68_,
       userentity0_.last_modified_by      AS last_mod4_68_,
       userentity0_.last_modified_date    AS last_mod5_68_,
       userentity0_.group_id              AS group_id6_68_,
       userentity0_.group_name            AS group_na7_68_,
       userentity0_.is_enrollment_updated AS is_enrol8_68_,
       userentity0_.is_federated          AS is_feder9_68_,
       userentity0_.name                  AS name10_68_,
       userentity0_.organization_id       AS organiz17_68_,
       userentity0_.ref_approval_group_id AS ref_app11_68_,
       userentity0_.reference_name        AS referen12_68_,
       userentity0_.status                AS status13_68_,
       userentity0_.uims_id               AS uims_id14_68_,
       userentity0_.user_status           AS user_st15_68_,
       userentity0_.version               AS version16_68_
FROM   user userentity0_
       INNER JOIN organization organizati1_
               ON userentity0_.organization_id = organizati1_.id
WHERE  organizati1_.name ='utopia'
       AND ( userentity0_.status =(?,?) 

when i take the query into db tool and passing the values i am getting the data. but while running from the application i am not getting the data. here i understood that i am able to generate the query properly but my values are not passing correctly. so could you please suggest how i can get my code return the data.

Maybe the implementation of it causes the issue...

Here, use this and let me know if it works

if (status != null && status.size()>0) {
    predicates.add(builder.in(root.get("status")).value(status));
}

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