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

method derivation for Specification<...>

Open ooraini opened this issue 2 years ago • 2 comments

This is a suggestion.

I was looking into using Specification on a repository. The combinators and/or were interesting, but building the primitive specification with the criteria API seemed rough. Then I thought the technology already exists with method derivation. Something like:

interface Products extends CrudRepository<....> { 
  Specification<Product> byName(String name);
  Specification<Product> byPriceLessThan(BigDecimal price);
)

The client can then combine them:

Products products = ...;
var spec = products.byName("bag").and(products.priceLessThan(BigDecimal.ONE));

ooraini avatar Feb 28 '23 22:02 ooraini

Thanks for your suggestion. This seems a neat idea at first glance, however it shows that this is an approach to address an underlying problem that seems to be solved better using custom implementations. Have you tried building such integration on your side?

Our entire infrastructure isn't based on Specifications. That is, why we're reluctant implementing such an feature because it would create a parallel approach to our existing infrastructure.

mp911de avatar Mar 14 '23 15:03 mp911de

I realize that the Specification API is only for JPA, but thinking more generally, method derivation in Spring data projects that use repositories(JDBC, JPA, Mongo) could benefit from a more composable API for filtering data. Not necessarily the current Specification interface as is.

I haven't seen the Spring data code for generating method derivation queries, so it might not be feasible, but whatever the implementation for the filtering part, if it could be factored out into a new API(or the existing Specification) such that the byX and findByX are generating quires based on the same data.

This would move filtering closer to the way we can do sorting today with Sort. Also, with the right generic type we might be able to have a HandlerMethodArgumentResolver for Specification(or whatever it's called).

ooraini avatar Mar 14 '23 16:03 ooraini