spring-data-jpa icon indicating copy to clipboard operation
spring-data-jpa copied to clipboard

Introduce `Specification.getParameters()`

Open quaff opened this issue 1 year ago • 1 comments
trafficstars

Add the ability to set parameter values for Specification

Fix GH-3192

quaff avatar Apr 26 '24 09:04 quaff

Closing as per #3192 (comment)

We discussed the topic as team and concluded that separation of the query and its parameters isn't a great programming model.

For safe SQL query execution, the query declaration and the parameters are typically split (SELECT foo FROM bar where baz = :baz) as that is the mode which queries are build and parameters are passed. In arrangements where queries are colocated with parameters for execution (e.g. jdbcTemplate.execute("SELECT … where baz = :baz", myBaz)), you can immediately realize the relationship and parameter requirement. We've been advocating a captive programming model for specifications in the following form:

var spec = userHasFirstname("Foo").or(userHasLastname("Bar")).or(userHasLastname("Baz"));
repository.findAll(spec);

I totally agree that, this PR colocate query and its parameters in single Specification, please see added tests.

quaff avatar Apr 26 '24 09:04 quaff

We declined passing on parameters in an earlier pull request #3192. Parameters require a lot more design and a well-thought mechanism to avoid clashes across multiple parameters. Especially allOf, and are candidates where named parameters can override each other ending up with an invalid query state.

Currently, we do not have the bandwidth to work on the parameters design. We have this request however on our radar.

mp911de avatar Apr 29 '24 08:04 mp911de

Especially allOf, and are candidates where named parameters can override each other ending up with an invalid query state.

SpecificationComposition will deny ambiguous parameter.

quaff avatar Apr 29 '24 09:04 quaff