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
2.98k stars 1.41k forks source link

Improve readability of querydsl queries by adding where helper [DATAJPA-267] #683

Closed spring-projects-issues closed 4 years ago

spring-projects-issues commented 11 years ago

Marcel Overdijk opened DATAJPA-267 and commented

As mentioned in http://blog.springsource.org/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ the querydsl api gives an an almost fluent english readable query.

E.g.

QCustomer customer = QCustomer.customer;
LocalDate today = new LocalDate();
BooleanExpression customerHasBirthday = customer.birthday.eq(today);
BooleanExpression isLongTermCustomer = customer.createdAt.lt(today.minusYears(2));
customerRepository.findAll(customerHasBirthday.and(isLongTermCustomer));

In my own project I added a static where() helper to make this even more readable. The the call would look like:

customerRepository.findAll(where(customerHasBirthday).and(isLongTermCustomer));

Not only it reads as "find all where" but also not each predicate is not in it's own brackets.

The static helper I used for this is:

public static BooleanBuilder where(Predicate predicate) {
    return new BooleanBuilder(predicate);
}

Please consider adding this to Spring Data.

Additionally this would also be more in line when using Specifications. It's the equivalent for http://static.springsource.org/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/domain/Specifications.html#where(org.springframework.data.jpa.domain.Specification).


Affects: 1.2 GA

1 votes, 1 watchers

spring-projects-issues commented 4 years ago

Jens Schauder commented

I don't think this adds enough benefit to carry its weight especially since anybody interested in having this might just add it themselves