Spring – JpaRepository Query Methods

Today we´re going to talk about the Query Methods of JpaRepositories. There are different approaches for defining database queries in JPA. One very familiar are the NamedQueries which are written in the JPQL.Close to this are the NativeQueries (which I wouldn´t recommend to you). These two types of Queries can also be implemented in a Spring environment. But there is another very confortable way. The QueryMethods. Many database queries are quite simple.

If you are already familiar with JPQL, Named- and NativeQueries you can skip the first chapter.

If you have a already a finished entity class and a working JpaRepository and just want to look at the QueryMethods, please visit Chapter 3.

Example Query

An example for such easy query would be “loading all user with the status active”.

JPQL

Using NamedQuery in an EJB / JPA context:

SELECT u FROM User u WHERE u.active = TRUE

Plain SQL

Using NativeQuery in an EJB / JPA context:

SELECT * FROM User WHERE active = 1

As you can see, in this example the queries look similiar. But just the query is the shortest part of defining the Named- or NativeQuery. More disadvantages would be

  • Hard to maintain
  • Write a lot of annotations above your entity
  • More (complex) code to call the queries
  • …I´m sure you´ll finde more disadvantages if you think about…

Spring QueryMethod

findByActiveTrue()

That´s it. Read again the disadvantages and think which we solved.

Entity and Repository Setup

But how is this cute little method doing all of the job? Let´s talk about that.

User Entity

Just add a simple entity like the following.

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstname;
    private String lastname;
    private LocalDate birthday;
    private boolean active;

    //getter and setter stuff
}

JpaRepository

To get the Spring Repository to work we need to extend JpaRepository to an interface that we have to define. To match my example, I will name the interface UserRepository.

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}

The JpaRepository brings some methods like findAll() by default. We have to parameterize the JpaRepository with our entity and the data type of the id parameter in our entity.

QueryMethods

Now, to come to the basic of this post, let´s get query methods to work.

To define such a method, you need to know which variable should be targeted in the WHERE condition. In our example it was the active parameter of the user entity. Because a boolean expression can only have two shapings Spring provides the possible values already in the method name. So we don´t have to give a parameter to the method. Please watch the cammel case

findByActiveTrue()
findBy<AttributeName><Value>

QueryMethod Survey

To get a good overview about all the possible methods I´d like to show you the table 2. from Spring Documentation.

Based on this, I´m sure you can now achieve many Queries without writing big Named- or NativeQueries

Conclusion

In this post we talked about, the benefits of QueryMethods towards Named- or NativeQueries.
After we looked at how the implementation of a simple query would look using JPQL or SQL, we wrote a QueryMethod. Based on the knowledge and explanation of this QueryMethod we considered a short overview about the possible looks of the QueryMethods.

 

So thanks for reading and have fun using the QueryMethods in Spring 

Feel free to share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.