Spring规范 - 谓词的结合

时间:2018-01-11 15:08:10

标签: spring jpa spring-data spring-data-jpa criteria

我需要一个可以过滤参数和构建查询的函数。我有4个参数因此,如果我尝试为每个条件实现查询,我将不得不编写16个(2^4)实现 - 这不是一个好主意。

我尝试使用Spring Data JPA中的接口Specification来改进我的代码,但是我无法创建谓词的连接。

Specification接口的实现:

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

测试不起作用:

@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);
    }

在测试之前,我在用户之后插入数据库:

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

结果我得到前四行。存储库仅匹配第一个参数“name”并且与“surname”不匹配。原因是什么?

(UserSearchCriteria与User:name,surname,city,country)具有相同的字段。

1 个答案:

答案 0 :(得分:3)

您正在使用单个if-else,因此每次只匹配一个条件。

尝试将其更改为一系列独立的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()]));
}