Query database from method name in Spring Data JPA

In my previous tutorial, I showed you all an overview of Spring Data JPA and made an example of using the findById() method available in the JpaRepository interface to query the database for the data in the primary key column and method findByName() to retrieve student information by name. As you saw, there is no class implementing the HelloRepository interface, but we still get the information we want. This is a great feature of Spring Data JPA, don’t you? 😀 To do that, you must follow the Spring Data JPA syntax in naming a method. How is it in detail? Let’s find out in this tutorial.

First, I will also create a Maven project as an example:

Query database from method name in Spring Data JPA

The structure of this project is similar to the preparatory steps we have done in the tutorial Overview about Spring Data JPA.

We will use Java 17 for this example:

Maven dependencies:

spring.xml:

persistence.xml:

Table student:

The database currently has student information as follows:

Query database from method name in Spring Data JPA

Student entity:

The StudentRepository class has the following content:

The application class also has the following content:

OK, the preparation steps are finished, now we go to the main topic of this tutorial.



Now, I’ll list all of the rules for naming a method in the Spring Data JPA, so that we do not need to implement the interface to get the data we need.

First of all, the name of the method, you must write it starting with one of the following names: find … By, read … By, query … By, count … By, and get … By. And then the attribute name of the entity you want to find.

Suppose, I now find student information in class as follows:

When you run, you will see the results as follows:

Query database from method name in Spring Data JPA

Or find the student named Khanh as follows:

Result:

Query database from method name in Spring Data JPA

If you want to limit the number of records returned, you can add the First or Top together with the number of records that you want to return before By in the name of the method in the above syntax.

If there is no record number passed, only 1 record is returned by default.

Eg:

Result:

Query database from method name in Spring Data JPA

If you want to find with many different conditions, then the method name must be associated with multiple attribute names of the entity and you must have multiple parameters for the method.

These parameters must be in the same order as the order of the attribute names in your method name.

For example, suppose you look for the student’s name and class, your method name should be findByNameAndClazz (String name, String clazz), the two parameter name and clazz must be in the same order as the method name.

Result:

Query database from method name in Spring Data JPA

We can also order the returned result by adding OrderBy…Asc (incremental) or OrderBy...Desc (descending) to the end of the method name.

The three dots are the property names of the entities we use to order.

For example:

Result:

Query database from method name in Spring Data JPA

Notice that: for each of the above examples, you will see the SQL statement that Spring Data JPA has generated. Look at that, you can understand more how Spring Data JPA 😀

 

Add Comment