The previous part of this tutorial taught us to pass method parameters to our query methods and described what kind of values we can return from them.

This blog post describes how we can create query methods by using the query generation from the method name strategy.

We will also implement a simple search function that has two requirements:

It must return todo entries whose title or description contains the given search term. The search must be case insensitive.

Let’s get started.

Additional Reading: If you are not familiar with Spring Data JPA, you should read the following blog posts before you continue reading this blog post: Spring Data JPA Tutorial: Introduction provides a quick introduction to Spring Data JPA and gives an overview of the Spring Data repository interfaces.

Spring Data JPA Tutorial: Getting the Required Dependencies describes how you can get the required dependencies.

Spring Data JPA Tutorial: Configuration describes how you can configure the persistence layer of a Spring application that uses Spring Data JPA.

Spring Data JPA Tutorial: Introduction to Query Methods describes how you can pass method parameters to your query methods and identifies the “legal” return values of Spring Data JPA query methods.

Creating Query Methods

The query generation from the method name is a query generation strategy where the invoked query is derived from the name of the query method.

We can create query methods that use this strategy by following these rules:

The name of our query method must start with one of the following prefixes: find…By, read…By, query…By, count…By, and get…By.

If we want to limit the number of returned query results, we can add the First or the Top keyword before the first By word. If we want to get more than one result, we have to append the optional numeric value to the First and the Top keywords. For example, findTopBy, findTop1By, findFirstBy, and findFirst1By all return the first entity that matches with the specified search criteria.

If we want to select unique results, we have to add the Distinct keyword before the first By word. For example, findTitleDistinctBy or findDistinctTitleBy means that we want to select all unique titles that are found from the database.

We must add the search criteria of our query method after the first By word. We can specify the search criteria by combining property expressions with the supported keywords.

If our query method specifies x search conditions, we must add x method parameters to it. In other words, the number of method parameters must be equal than the number of search conditions. Also, the method parameters must be given in the same order than the search conditions.

We must set the return type of the query method by following the rules that were described in the previous part of this tutorial.

My "Test With Spring" course helps you to write unit, integration, and end-to-end tests for Spring and Spring Boot Web Apps: CHECK IT OUT >>

The following examples demonstrate how we can create simple query methods by using the query generation from the method name strategy:

Example 1: If we want to create a query method that returns the todo entry whose id is given as a method parameter, we have to add one of the following query methods to our repository interface:

import org.springframework.data.repository.Repository; import java.util.Optional; interface TodoRepository extends Repository<Todo, Long> { /** * Returns the found todo entry by using its id as search * criteria. If no todo entry is found, this method * returns null. */ public Todo findById(Long id); /** * Returns an Optional which contains the found todo * entry by using its id as search criteria. If no to entry * is found, this method returns an empty Optional. */ public Optional<Todo> findById(Long id); }

Example 2: If we want to create a query method that returns todo entries whose title or description is given as a method parameter, we have to add the following query method to our repository interface:

import org.springframework.data.repository.Repository; import java.util.List; interface TodoRepository extends Repository<Todo, Long> { /** * Returns the found todo entry whose title or description is given * as a method parameter. If no todo entry is found, this method * returns an empty list. */ public List<Todo> findByTitleOrDescription(String title, String description); }

Example 3: If we want to create a query method that returns the number of todo entries whose title is given as a method parameter, we have to add the following query method to our repository interface:

import org.springframework.data.repository.Repository; interface TodoRepository extends Repository<Todo, Long> { /** * Returns the number of todo entry whose title is given * as a method parameter. */ public long countByTitle(String title); }

Example 4: If we want to return the distinct todo entries whose title is given as a method parameter, we have to add the following query method to our repository interface:

import org.springframework.data.repository.Repository; import java.util.List; interface TodoRepository extends Repository<Todo, Long> { /** * Returns the distinct todo entries whose title is given * as a method parameter. If no todo entries is found, this * method returns an empty list. */ public List<Todo> findDistinctByTitle(String title); }

Example 5: If we want to to return the first 3 todo entries whose title is given as a method parameter, we have to add one of the following query methods to our repository interface:

import org.springframework.data.repository.Repository; import java.util.List; interface TodoRepository extends Repository<Todo, Long> { /** * Returns the first three todo entries whose title is given * as a method parameter. If no todo entries is found, this * method returns an empty list. */ public List<Todo> findFirst3ByTitleOrderByTitleAsc(String title); /** * Returns the first three todo entries whose title is given * as a method parameter. If no todo entries is found, this * method returns an empty list. */ public List<Todo> findTop3ByTitleOrderByTitleAsc(String title); }

Let’s move on and create the query method that fulfils the requirements of our search function.

Implementing the Search Function

We can implement the search function by following these steps:

Create a query method whose name starts with the prefix findBy. Ensure that the query method returns todo entries whose description contains the given search term. We can do this by appending the property expression: Description and the keyword: Contains to the method name. Configure the query method to return the information of a todo entry if the previous (2) or the next (4) search condition is true. We can do this by appending the keyword: Or to the method name. Ensure that the query method returns todo entries whose title contains the given search term. We can do this by appending the property expression: Title and the keyword: Contains to the method name. Ensure that the search is case insensitive. We can do this by appending the keyword: AllIgnoreCase to the method name. Add two method parameters to the query method: Spring Data JPA uses the descriptionPart method parameter when it ensures that the description of the returned todo entry contains the given search term. Spring Data JPA uses the titlePart method parameter when it ensures that the title of the returned todo entry contains the given search term. Set the type of the returned object to List<Todo>.

The source code of our repository interface looks as follows:

import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import java.util.List; interface TodoRepository extends Repository<Todo, Long> { List<Todo> findByDescriptionContainsOrTitleContainsAllIgnoreCase(String descriptionPart, String titlePart); }

Let’s move on and find out when we should create our query methods by using the query generation from the method name strategy.

When Should We Use the Query Generation From the Method Name Strategy?

This query generation strategy has the following benefits:

Creating simple queries is fast.

The method name of our query method describes the selected value(s) and the used search condition(s).

This query generation strategy has the following weaknesses:

The features of the method name parser determine what kind of queries we can create. If the method name parser doesn’t support the required keyword, we cannot use this strategy.

The method names of complex query methods are long and ugly.

There is no support for dynamic queries.

When we think about the pros and cons of this strategy and take a second look at our examples, it becomes clear that the length of our query method name determines whether or not we should use this strategy.

I am not a big fan of “super long” method names because they become unreadable very fast. If we compare the method name: findById with the method name: findByDescriptionContainsOrTitleContainsAllIgnoreCase, we notice that first one is very easy to read. The second method name is not nearly as easy to read as the first one, but it is not impossible to read either (yet). It is a borderline case.

Because I want to write code that is easy to read, I think that we should use this strategy only when we are creating simple queries that have only one or two search conditions.

Let’s move on and summarize what we learned from this blog post.



My "Test With Spring" course helps you to write unit, integration, and end-to-end tests for Spring and Spring Boot Web Apps: CHECK IT OUT >>

Summary

This blog post has taught us the following things:

If we want to use the query generation by method name strategy, the name of our query method must start with a special prefix.

We can select unique results by using the Distinct keyword.

We can limit the number of returned query results by using either the Top or the First keyword.

We can create search conditions by using property expressions and the keywords supported by Spring Data JPA.

We should use the query generation from the method name strategy only when our query is simple and has only one or two search conditions.

The next part of this tutorial describes how we can create query methods by using the @Query annotation.

P.S. You can get the example application of this blog post from Github.