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