Spring Specification - conjunction of Predicates
Asked Answered
S

1

4

I need a function that will be filter parameters and build query. I have 4 parameters therefore if I would try to implement query for each condition I would have to write 16 (2^4) implementations - it's not good idea.

I try to improve my code with interface Specification from Spring Data JPA but I cannot create conjunction of predicates.

Implementation of Specification interface :

public class UserSpecification implements Specification<User> {

    private final UserSearchCriteria criteria;

    public UserSpecification(UserSearchCriteria criteria) {
        this.criteria = criteria;
    }

    @Override
    public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        final List<Predicate> predicates = new ArrayList<>();
        if (criteria.getName() != null) {
            final Predicate name = builder.equal(root.<String>get("name"), criteria.getName());
            predicates.add(name);
        } else if (criteria.getSurname() != null) {
            final Predicate surname = builder.equal(root.<String>get("surname"), criteria.getSurname());
            predicates.add(surname);
        } else if (criteria.getCity() != null) {
            final Predicate city = builder.equal(root.<String>get("city"), criteria.getCity());
            predicates.add(city);
        } else if (criteria.getCountry() != null) {
            final Predicate country = builder.equal(root.<String>get("country"), criteria.getCountry());
            predicates.add(country);
        }
        return builder.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}

And test doesn't work :

@Test
    @Sql(
            scripts = "classpath:sql/specification.sql",
            executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD
    )
    public void specificationTest() {
        // given
        final UserSearchCriteria criteria = UserSearchCriteria.builder()
                .name("john")
                .surname("smith")
                .build();

        final UserSpecification specification = new UserSpecification(criteria);

        // when
        final List<User> result = userRepository.findAll(specification);
        userRepository.flush();

        // then
        assertThat(result).hasSize(3);
    }

Before test I insert to database following users:

INSERT INTO users (id, name, surname, city, country) VALUES (1, 'john', 'smith', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (2, 'john', 'smith', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (3, 'john', 'smith', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (4, 'john', 'abc', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (5, 'abcd', 'abc', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (6, 'abcd', 'abc', null, null);

And as a result I get first four rows. Repository matches only first parameter "name" and doesn't match "surname". What is the cause?

(UserSearchCriteria has the same fields as User: name, surname, city, country).

Salesroom answered 11/1, 2018 at 15:8 Comment(0)
C
6

You are using a single if-else, so only one conditions is matched each time.

Try changing it to a sequence of standalone ifs:

@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    final List<Predicate> predicates = new ArrayList<>();
    if (criteria.getName() != null) {
        final Predicate name = builder.equal(root.<String>get("name"), criteria.getName());
        predicates.add(name);
    }
    if (criteria.getSurname() != null) {
        final Predicate surname = builder.equal(root.<String>get("surname"), criteria.getSurname());
        predicates.add(surname);
    }
    if (criteria.getCity() != null) {
        final Predicate city = builder.equal(root.<String>get("city"), criteria.getCity());
        predicates.add(city);
    }
    if (criteria.getCountry() != null) {
        final Predicate country = builder.equal(root.<String>get("country"), criteria.getCountry());
        predicates.add(country);
    }
    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
}
Charlean answered 11/1, 2018 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.