Dear spring devs,
I'd like to suggest the following change in interface Specification<T>:
Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);
to
Predicate toPredicate(From<?, T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);
Because From<T, U> is the supertype of both Root<T> and Join<T, U>, it would allow better reuse of specifications when joining tables.
Our use case is the following, we have a OneToMany relation
Table CarMaker <>---> Table Car
We have already made Specification factory methods from Request DTOs for entities CarMaker and Car.
We would like to reuse these 2 methods when filtering on both CarMaker and Car:
Specification<CarMaker> filterCarMakers(CarMakerRequestDTO request) { ... }
Specification<Car> filterCars(CarRequestDTO request) { ... }
Specification<CarMaker> filterCarMakersAndCars(CarMakerAndCarAggregatedRequestDTO request) {
return filterCarMakers(request.getCarMakerRequest())
.and((r, q, c) -> filterCars(request.getCarRequest()).toPredicate(r.join("cars", JoinType.INNER), q, c));
}
see also: https://stackoverflow.com/a/46955740/6705221
Dear spring devs,
I'd like to suggest the following change in interface
Specification<T>:Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);to
Predicate toPredicate(From<?, T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);Because
From<T, U>is the supertype of bothRoot<T>andJoin<T, U>, it would allow better reuse of specifications when joining tables.Our use case is the following, we have a OneToMany relation
Table CarMaker <>---> Table Car
We have already made Specification factory methods from Request DTOs for entities
CarMakerandCar.We would like to reuse these 2 methods when filtering on both CarMaker and Car:
see also: https://stackoverflow.com/a/46955740/6705221