r/SpringBoot • u/Flea997 • Jul 26 '25
Discussion Why I hate Query by Example and Specifications in Spring Data JPA
Beyond the problem of coupling your repository interfaces methods to JPA-specific classes (which defeats the whole purpose of abstraction), Query by Example and Specifications have an even worse issue:
They turn your repository into a generic data dumping ground with zero business control
When you allow services to do:
User exampleUser = new User();
exampleUser.setAnyField("anything");
userRepository.findAll(Example.of(exampleUser));
// or
userRepository.findAll(Specification.where(...)
.and(...).or(...)); // any crazy combination
Your repository stops being a domain-driven interface that expresses actual business operations like:
List<User> findActiveUsersByRole(Role role);
List<User> findUsersEligibleForPromotion();
And becomes just a thin wrapper around "SELECT * WHERE anything = anything."
You lose:
- Intent - What queries does your domain actually need?
- Control - Which field combinations make business sense?
- Performance - Can't optimize for specific access patterns
- Business rules - No place to enforce domain constraints
Services can now query by any random combination of fields, including ones that aren't indexed, don't make business sense, or violate your intended access patterns.
Both approaches essentially expose your database schema directly to your service layer, making your repository a leaky abstraction instead of a curated business API.
Am I overthinking this, or do others see this as a design smell too?
16
u/0xFatWhiteMan Jul 26 '25
This looks awful.
Why are so many people and frameworks obsessed with avoiding SQL ?
SQL is awesome.
6
u/titanium_hydra Jul 26 '25
IMO is not SQL people are trying to avoid, it’s the boilerplate bindings between a relational and object model.
I don’t think any modestly skilled programmer has any problem with sql
0
-5
u/flavius-as Jul 26 '25
I agree with both points: sql is awesome and boilerplate is a pain.
Luckily we have LLMs now who are great at mapping the shit out of anything.
You could even give them the SELECT statement, and ask them to:
Generate the mapping code
Generate the corresponding UPDATE, INSERT, DELETE as per requirements
Generate the mapping code for those
1
u/Flea997 Jul 26 '25
I usally write my SQL queries, I just started a new project and wanted to see what people use nowadays. Didn't click for me
1
10
u/WaferIndependent7601 Jul 26 '25
I don’t understand your problem. You can search for not indexed fields if you want. But that’s an issue you always have.
Do you have a real world problem or is it just some thoughts?
0
u/Flea997 Jul 26 '25
Just some thoughts.
If you make your repository have a
findBy(Specification spec)method you probably don't need any other method from that repository making it kind of useless 🤔2
u/WaferIndependent7601 Jul 26 '25
That’s the good thing about programming: you can do awesome stuff but also use the language wrong and make your program suck.
Only allow specifications where you have an index on.
It’s like „why can I use a ‚select … where‘ in sql when there is no index“. Its your work to not make these mistakes
1
u/Flea997 Jul 26 '25
With
findAll(Specification)the repository layer becomes a thin pass-through — just a generic method. To me the repository Interface should be a contract on how you can query data, but with Specifications you can not enforce any control anymore2
u/WaferIndependent7601 Jul 26 '25
You’re in control. What do you expect? What would be better? Don’t use specifications. And specifications also festive how you get your data.
I really don’t understand your problem.
2
u/MaDpYrO Jul 26 '25
"Only allow specifications you have an index on"
This is the entire point of keeping database logic encapsulated inside your repository. Otherwise the repository is just dumb boilerplate.
Expose a method in your repository that does what you want for the specific case. Don't mutate sql from outside your repository.
-1
u/WaferIndependent7601 Jul 26 '25
Ok so if you want to search for name address and birth date, that will be one method. When you’re searching for phone number, too, you need another method? So in the end you’ll have 100 methods if you want every combination searchable? Or did I get you wrong here?
2
u/MaDpYrO Jul 26 '25
No you can build a single method with nullable arguments and build it accordingly. It's also not technically wrong to use specification for certain cases, but you shouldn't offload logic too much into it.
Edit: I practice if you're searching by that many fields it may be done across several joins too. That. Means you need top optimizer for the case where some joins maybe aren't necessary. You can't do that if you're just changing all the where statements for a single query. These optimisations tend to matter more in the long run than people seem to believe. (unless your user base is tiny of course)
-3
u/WaferIndependent7601 Jul 26 '25
And the nullables will be what? In the end it will be a specification. And you’ll run it against the repo.
I don’t talk about multiple tables.
I’m out here. You have no idea what you’re talking about
2
u/MaDpYrO Jul 26 '25
No, a specification would allow you to mutate the query from outside the repository which you usually wouldn't want.
It's also fair to use a specification inside your implementation. Personally I wouldn't want to allow changes to an sql query outside the repository, but I not saying there aren't cases where it would be the pragmatic choice.
3
u/stao123 Jul 26 '25
How would you implement a generic filter where the user decides which fields should be filtered in which combinations?
1
u/Flea997 Jul 26 '25
That's how it all started, except that I want to filter for specifically 4 parameters, not every
1
u/stao123 Jul 26 '25
How do you know which of the 4 parameters your user wants to filter?
1
u/Flea997 Jul 26 '25
What do you mean? Four Optional parameters
1
u/stao123 Jul 26 '25
Ok und which repository function will you use? If you dont know which parameters will be filtered and you cant implement all possible combinations you will HAVE to rely on a generic query builder (like the findBy Specification)
1
u/Flea997 Jul 26 '25
I think Specifications are suited for this. What I don't like is that Specification is too generic and the layer above repository can make a query for any arbitrary field, while I want my data to be filtered only by specific fields
1
u/TooLateQ_Q Jul 26 '25
You could still have both by moving the mapping to query by example inside the repository.
1
u/Flea997 Jul 26 '25 edited Jul 26 '25
Not sure if I understood (do you mean by writing a custom repository method implementation?), could you write a minimal example?
2
u/MaDpYrO Jul 26 '25
It's difficult to pinpoint exactly because for some filtering cases specification works best.
But if your logic is getting too crazy inside it, you should definitely make a custom repository method.
2
1
1
1
u/Enough_Arrival_7335 Apr 25 '26
I think the core issue is not QBE/Specification themselves, but where teams put boundaries.
If repositories expose “query anything” APIs, you lose:
- predictable access patterns
- index-aware query design
- business-level constraints
A practical compromise:
- keep repository methods use-case oriented (explicit names, explicit projections)
- use Specifications sparingly behind curated methods
- move hot paths to explicit JPQL/native SQL when needed
This keeps flexibility without turning repositories into unconstrained query builders.
-1
1
Jul 27 '25
[removed] — view removed comment
2
u/Flea997 Jul 28 '25
the problem of Query annotation in spring data jdbc is that
Note that String-based queries do not support pagination nor accept Sort, PageRequest, and Limit as a query parameter as for these queries the query would be required to be rewritten.
1
17
u/Misfiring Jul 26 '25
Repositories are not supposed to contain business rules, they are there for services to utilize. Your services should be the one to contain business logics including what data to fetch. Repositories are gateways to your database and at most should contain only basic, generic rules for that entity.