Open jobayle opened 1 year ago
From an API perspective, Root
declares one method more than From
does, and it denotes a more specific type. Generally speaking, From
could be a better alternative than Root
to improve reuse of specifications. However, specifications weren't designed with reuse in mind and we have no way to introduce a non-breaking change considering the amount of existing specification implementations.
This could be a topic worth revisiting for Spring Data JPA 4.0.
Another approach here could be the introduction of a functional-style composition API to build and reuse specification-like fragments. Anyone willing to experiment with that kind of approach can take a spike and we're happy to keep the discussion going.
when we are at it, why not Path<X>
?
With a Path
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
CarMaker
andCar
. We would like to reuse these 2 methods when filtering on both CarMaker and Car:see also: https://stackoverflow.com/a/46955740/6705221