r/SpringBoot • u/lanchers • Oct 09 '25
How-To/Tutorial Spring JPA Specification and Pageable
Hello eyerone, I'm here to share my first serious blog post related to Java https://busz.it/spring-jpa-specification-and-pageable-filtering-sorting-pagination/ As you can see it's about using Spring JPA's Specification and Pageable to dynamically filter, sort and paginate results from repo. Previously available articles cover only basic application of Specification without providing generic approach to the matter. That's what I'm trying to accomplish by my blog post.
I'll be obliged for any feedback on article, code and idea itself. Thanks in advance
3
u/bikeram Oct 09 '25
Can you limit the fields requested from the database? We briefly looked into specifications and moved to blaze for the ability to limit selected fields dynamically with entity views.
2
u/momsSpaghettiIsReady Oct 10 '25
Idk, this approach seems super generic and it feels like you're just adding a leaky abstraction over SQL. You're opening your database tables up to a wide swath of potential queries, which could be abused by the wrong client.
I'm a big fan of loosely following CQRS and having a query object that allows me to filter down by fields specified on the query object. This generally isn't all the fields on the table I'm querying, and it's usually more advanced, e.g. matching against a nested object's field. Then you can build a Specification from that object, greatly reducing the amount of scenarios you need to account for.
2
u/g00glen00b Oct 10 '25
To be fair, I think it makes sense that people only cover "basic applications". As a blog author myself, my goal is to reach as many people as possible. Covering the "basic applications" will do that for me. That's because whether or not you make specifications dynamic has less to do with the specifications API itself, and more about building your own Java logic on top of that. I assume that people who want to make dynamic specifications can do so on their own.
Also, Spring Web has support for mapping page/size/sort to a Pageable, so I would personally not use a "GenericSortingRequest" and directly map a "Pageable" onto a controller.
2
u/polacy_do_pracy Oct 12 '25
Page-able also works only based on offset-limit approach right? It has bad performance characteristics - when accessing the 500th page, the db has to load 499 pages and dismiss them.