spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.01k stars 1.42k forks source link

Dynamic query by specification builder [DATAJPA-1006] #1352

Closed spring-projects-issues closed 6 years ago

spring-projects-issues commented 7 years ago

Hao Wen opened DATAJPA-1006 and commented

Although spring data JPA support specification query, but it still hard to do the dynamic query base on specifications.

Two cons:

  1. duplicate code between specifications.
  2. haven't simplify the specification adoption logic.

Simplify example:

public Page<Person> findAll(SearchRequest request) {
    Specification<Person> specification = Specifications.<Person>builder()
            .eq(StringUtils.isNotBlank(request.getName()), "name", request.getName())
            .gt(Objects.nonNull(request.getAge()), "age", 18)
            .between("birthday", new Range<>(new Date(), new Date()))
            .like("nickName", "%og%", "%me")
            .build();

    return personRepository.findAll(specification, new PageRequest(0, 15)); 
}

more refer to: jpa-spec


Affects: 1.11 M1 (Ingalls)

Reference URL: https://github.com/wenhao/jpa-spec

spring-projects-issues commented 7 years ago

Oliver Drotbohm commented

As much as I appreciate your efforts to put a dynamic query API on top of a dynamic query API, I don't think it's what we want to get into. There's already a decent option in the form of Querydsl out there that also has the advantage of being tied to JPA but work on other stores.

That said, if there's anything we can do to make your library integrate more seamless with Spring Data JPA, we can definitely take a look at that

spring-projects-issues commented 7 years ago

Hao Wen commented

make sense, closing this card now