Building queries
There are three options we can use to build queries with Spring Data JPA: query methods, JPA Criteria API, and Querydsl. During this section, we will learn how to use them and start the implementation of our search function. We will also take a look at the pros and cons of each option and receive concrete recommendations concerning the selection of the correct query creation technique.
Before we can move forward, we have to add a search()
method, which is used as a starting point of our search function, to the ContactService
interface. The signature of the search()
method looks like the following code snippet:
public List<Contact> search(String searchTerm);
Query methods
The simplest way to create queries with Spring Data JPA is to use query methods. Query methods are methods that are declared in the repository interface. There are three techniques we can use to create query methods:
Query generation from method name
Named Queries
The
@Query
annotation