Derived Query Methods of Spring Data JPA

Use Derived Query Methods of Spring Data JPA Repositories instead of writing your own queries.

For the simple jpa queries, we generally use @Query annotation to make the queries, but the best practice says that we should use Derived Query Methods instead. Most developers hardly use one or two derived query methods like findById, and findAll but we can use many of them and get the job done so quickly. Check out below :

 

  1. Structure of Derived Query Methods :

Derived methods have two main parts separated by the By keyword :

List<User> findByName(String name)

The first part – find is the introducer and the rest – ByName is the criteria. You can make your own criteria according to your model’s properties such as findByName, findByAge, findByEmail, etc.

Spring Data JPA supports many introducers to find, read, query, count, and get. So, we could have done countByAge(int age) and we would get a count of the users having that age.

We can also use First, Top, or Distinct to remove duplicates like

List<User> findTop3ByAge(),  and we’ll get the list of the top 3 users by age, isn’t it awesome?

 

  1. Equality Condition Keywords :

We can add Is or Equals for readability :

            List<User> findByNameIs(String name);

            List<User> findByNameEquals(String name);

            When we need to express inequality instead :

 

            List<User> findByNameIsNot(String name);

 

We can also use the IsNull keyword to add NULL criteria in the query :

            List<User> findByNameIsNull();

            List<User> findByNameIsNotNull();

 

We can also use True and False keywords to add equality conditions for boolean types:

            List<User> findByActiveTrue();

            List<User> findByActiveFalse();
  1. Similarity Condition Keywords :

We can find entries that start with a value using StartingWith :

 List<User> findByNameStartingWith(String prefix);

 

If we want the entries that end with a value, we can use EndingWith :

List<User> findByNameEndingWith(String suffix);

 

 We can find entries containing a value using Containing :

List<User> findByNameContaining(String infix);

 

  1. Comparison Condition Keywords :

We can use LessThan, LessThanEqual, GreaterThan, and GreaterThanEqual keywords in order to get the data by comparing the records, for example :

 

List<User> findBySalaryLessThan(Integer salary);

List<User> findBySalaryLessThanEqual(Integer salary);

List<User> findBySalaryGreaterThan(Integer salary);

List<User> findBySalaryGreaterThanEqual(Integer salary);

 

More exciting is that we can find entries between two parameters using Between keyword, for example :

We can find Users who are between two salaries :

List<User> findBySalaryBetween(Integer startSalary, Integer endSalary);

For the date and time, we can use the Before and After keywords:

List<User> findByBirthDateAfter(DateTime birthDate);

List<User> findByBirthDateBefore(DateTime birthDate);

 

  1. Multiple Condition Expressions :

The most amazing thing is here, We can combine multiple expressions by using And and Or keywords, take a look :

 

List<User> findByNameOrSalary(String name, Integer salary);

 List<User> findByNameAndEmail(String name, String email);

 List<User> findByEmailOrBirthDateOrSalaryAndActive(String email, DateTime birthDate, Integer Salary, Boolean active);

 

We can combine as much expression as we need.

  1. Sorted Data :

            We can get sorted data by using OrderBy keyword :

            List<User> findByNameOrderByName(String name);

            List<User> findByNameOrderByNameAsc(String name);

            List<User> findByNameOrderByNameDesc(String name);

 

 

 

Leave a Reply