Thursday, September 19, 2019

Spring Data Repositories: CrudRepository, PagingAndSortingRepository and JpaRepository

In the last article, we have learned about the benefits and significance of using Spring Data JPA. We have also seen how it has eliminated the need of writing boilerplate code in order to get the working JPA repository without much hassle. Today we would have a discussion on different types of Spring Data Repository interfaces, it's functionality and basic steps required for building a repository.

1. Introduction


The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. Spring Data JPA provides various types of repository interfaces which could be extended depending upon the requirements to create our custom repository interface(not the implementation). Let's start our discussion.

2. Spring Data Repositories


We would be using below Repository interfaces to create our custom Repository interface:
  • CrudRepository extends Repository interface and should be used for generic CRUD operations on a repository.
  • PagingAndSortingRepository extends CRUDRepository interface and should be used when one needs to retrieve entities using the pagination and sorting abstraction.
  • JpaRepository extends PagingAndSortingRepository & QueryByExampleExecutor interface and should be used when one has the requirement to delete entities in a batch or flushing the persistence context which could be either flushing all pending changes to the database or saving an entity and flushing the changes instantly.
That means, the central interface in Spring Data repository abstraction is Repository & JpaRepository contains the full API of CrudRepository and PagingAndSortingRepository. But one should choose the repository interface based on their intended requirement. 

3. Steps for building a repository 


Below are the steps for building a repository interface with an example:
Step 1: Map the required POJO.
Step 2: Declare our domain class-specific custom interface extending Repository interface or one of its subinterfaces and type it to the domain class that it would handle.
public interface UserRepository extends CrudRepository<User, Long> { … }
Step 3: Declare query methods on the interface.
List<User> findByLastname(String lastname);  //Inferred Query
One could write inferred, JPQL or Native queries as per their requirements. Above is the example of the inferred query where one doesn't need to tell Spring Data what to do since it automatically infers the SQL query from the name of the method name.
Step 4: Configure Spring to scan for repository interfaces and create implementations.
Step 5: Get the repository instance injected in our Service class and then use it.
public class YourService {

  @Autowired
  private UserRepository repository;

  public void doSomething() {
    List<User> users = repository.findByLastname("Agrawal");
  }
}

4. Conclusion


I am sure you all would save a lot your effort and time in implementing your data access layers if you are using Spring Data Repositories. Just remember when to use which Spring Data Repository interface and you are good to go. Happy Learning!!

No comments:

Post a Comment