How to Create Dynamic SQL Queries with Spring Boot

Transcript:

Hi friends! In this video, we will learn how to create dynamic SQL queries in Spring Boot with Spring Data JPA. I will use a project I created for this tutorial. You can find it on GitHub. It is a minimalistic bookshop application. The main entity class is ‘book,’ and we also have some auxiliary classes such as ‘category,’ ‘format,’ and ‘language.’

They also have the book repository and book service. In a real-world bookshelf application, users apply multiple filters to find the book they need. But we don't want to write separate queries for various combinations of these filters, right? This is where dynamic SQL queries come into play.

As the name suggests, they enable us to create SQL queries dynamically based on the user input. To build dynamic SQL queries with JPA, we can use the ‘Specification’ interface. It helps us build multiple predicates over the entity. These predicates can be combined to create queries with flexible WHERE clauses.

To make use of JPA specification, let's make our book repository interface extend the JPA specification executor. Here it is. Our book repository extends both the JPA repository and specification. Now, we need to create a specification class. Let's call it BookSpecification.

Here, we will create our filters, which are going to be custom specifications. Let’s start with the first method and define a filter for category. We need to return the specification for the Book entity class and call this method hasCategoryName. We pass a String for the category name and write a return statement. We return the root, which is our book, along with the query and criteriaBuilder. The criteriaBuilder is the most important part as it provides numerous methods.

We use equal() and pass the root (our book), get the category, and then extract its name. This will be our filter. The book entity has a category field, and the category has a name field. We extract the category and then the name we are looking for.

But in most cases, we need multiple filters, not just one. So let’s add additional filters: price, language, and format. We start with the price filter by creating a method called hasPriceLessThanOrEqualTo and passing the price parameter. The logic is similar—we retrieve the price field from the root and use the lessThanOrEqualTo method from the criteriaBuilder.

Currently, we are using strings to reference entity fields, such as category name or price. While this works, it's not the safest approach because changes in field names can break the specification. Instead, we could use the JPA static model generator for type-safe queries, but we will keep it simple in this demo.

Our filters are ready. Now, let's move to the book service and write a method for finding books based on the filters. Users may apply none, some, or all filters, but we only need one method to handle all these cases. We pass String categoryName, String languageName, String formatName, and a price parameter.

We create a specification called spec, initially set to null to handle cases where no filters are applied. Then, we check each parameter—if it's not null, we add it to the specification using the and method with the corresponding filter from BookSpecification.

Finally, we return the list of books using bookRepository.findAll(spec). If the specification is empty, we return all books. Otherwise, we return a filtered list based on the applied criteria.

Now, it’s time to test it. In the book service test, I have pre-written some tests to speed up the demo. The setup is simple: we first find books by category while passing null for other filters. We test different filter combinations, including category and format, category and language, and all four filters together. We also check that an empty list is returned when there are no matches and verify that all books are returned when no filters are applied.

Let’s run the tests. All tests have passed! And that’s how we create dynamic SQL queries in Spring Boot. Don't forget to like this video, subscribe to our channel, and see you next time!

Summary

This video demonstrates how to create dynamic SQL queries in Spring Boot using Spring Data JPA and the Specification interface for flexible filtering. Instead of writing multiple separate queries for different filter combinations, conditions like category, price, language, and format can be dynamically combined. Finally, tests confirm that the method correctly filters data or returns all books if no filters are applied.

About Catherine

Java developer passionate about Spring Boot. Writer. Developer Advocate at BellSoft

Social Media

Videos
card image
Oct 16, 2025
All 7 Java Garbage Collectors Explained

In this complete guide to Java garbage collection, you will learn how the JVM memory model works, understand the differences between the Serial, Parallel, G1, ZGC, Shenandoah, CMS, and Epsilon collectors, and determine which garbage collector is best suited for your application's performance — from single-threaded programs to massive terabyte-scale heaps.

Videos
card image
Oct 7, 2025
Master Java Profiling in 2025: Tools, Techniques, and Real-World Tips

In this complete guide to Java profiling, you will learn sampling and instrumentation techniques, compare the 7 best tools (JFR, VisualVM, Async Profiler, JProfiler, YourKit, Digma.ai, New Relic), and master how to detect memory leaks and analyze CPU usage.

Further watching

Videos
card image
Nov 6, 2025
Docker Container Image Security: 13 Best Practices

This video presents 13 practical recommendations for reducing your attack surface and detecting malicious activity more quickly. You’ll learn how to create simple, immutable, and deterministic images using multi-stage builds, distroless bases, and non-root users. We cover SBOM generation with Syft, provenance verification with Cosign, CVE scanning workflows, and secret management strategies. From choosing LTS base images like Alpaquita Linux to implementing host-level protections, these practices will help you confidently deliver secure containers. It’s ideal for Java developers, DevOps engineers, and architects building production-grade infrastructure.

Videos
card image
Oct 31, 2025
Vaadin Tutorial: From Spring Boot to Beautiful UI Fast

In this guide, I’ll show you how to build a fully functional Java application with authentication, data tables, filters, and a custom cyberpunk theme using Vaadin.

Videos
card image
Oct 23, 2025
Top 7 JavaFX Testing Mistakes You Need To Avoid!

Stop making these common JavaFX testing mistakes! No more random NullPointerExceptions or deadlocks — in this video, we’ll show you how to fix the 7 most frequent TestFX issues when testing JavaFX applications. Learn how to work with FX threads, integrate with Spring Boot, avoid event-queue race conditions, handle pixel-level test differences, set up headless continuous integration with Monocle, and properly separate business logic from UI tests.