Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specification<T>.toPredicate(...) Change type of first parameter to From<?, T> #2761

Open
jobayle opened this issue Jan 12, 2023 · 4 comments
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement

Comments

@jobayle
Copy link

jobayle commented Jan 12, 2023

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 12, 2023
@mp911de
Copy link
Member

mp911de commented Jan 12, 2023

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.

@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Jan 12, 2023
@mp911de mp911de added status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 23, 2023
@mp911de
Copy link
Member

mp911de commented Jan 24, 2023

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.

@mp911de mp911de removed the for: team-attention An issue we need to discuss as a team to make progress label Jan 24, 2023
@bukajsytlos
Copy link

when we are at it, why not Path<X>?

@svenmeier
Copy link

With a Path you would not be able to join or correlate, so From<T,T> seems to be a nice fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants