Writing custom query methods

In the previous sections, we looked at the CrudRepository and PagingAndSortingRepository interfaces. We looked at the different methods that they provided by default; but Spring Data does not stop here. It defines a few patterns that allow you to define custom query methods. In this section, we will look at examples of some of the options that Spring Data provides to customize your query methods.

We will start with examples related to finding rows matching specific attribute values. The following example shows different methods in order to search for the User by their name:

    public interface UserRepository 
extends PagingAndSortingRepository<User, Long> {

List<User> findByName(String name);
List<User> findByName(String name, Sort sort);
List<User> findByName(String name, Pageable pageable);

Long countByName(String name);

Long deleteByName(String name);

List<User> removeByName(String name);
}

Important things to note are as follows:

  • List<User> findByName(String name): The pattern is findBy followed by the name of the attribute that you want to query by. The value of the attribute is passed in as a parameter.
  • List<User> findByName(String name, Sort sort): This method allows you to specify a specific sort order.
  • List<User> findByName(String name, Pageable pageable): This method allows the use of pagination.
  • As well as find, we can also use read, query, or get to name the methods, for example, queryByName instead of findByName.
  • Similar to find..By, we can use count..By to find the count, and delete..By (or remove..By) to delete records.

The following example shows how to search by attributes of a containing element:

    List<User> findByTodosTitle(String title);

The user contains Todos. Todo has the title attribute. To create a method to search a user based on the title of the todo, we can create a method by the name findByTodosTitle in UserRepository.

The following examples show a few more variations that are possible with findBy:

public interface TodoRepository extends CrudRepository<Todo, Long> {

List<Todo> findByTitleAndDescription(String title, String description);

List<Todo> findDistinctTodoByTitleOrDescription(String title,
String description);

List<Todo> findByTitleIgnoreCase(String title);

List<Todo> findByTitleOrderByIdDesc(String title);

List<Todo> findByIsDoneTrue();

}

Important things to note are as follows:

  • findByTitleAndDescription: Multiple attributes can be used to query.
  • findDistinctTodoByTitleOrDescription: Find distinct rows.
  • findByTitleIgnoreCase: Illustrates the use of the ignore case.
  • findByTitleOrderByIdDesc: Illustrates an example of specifying a specific sort order.

The following example shows how to find a specific subset of records using find:

    public interface UserRepository 
extends PagingAndSortingRepository<User, Long> {
User findFirstByName(String name);
User findTopByName(String name);
List<User> findTop3ByName(String name);
List<User> findFirst3ByName(String name);
}

Important things to note are as follows:

  • findFirstByName, findTopByName: Queries for the first user
  • findTop3ByName, findFirst3ByName: Finds the top three users
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset