Search⌘ K
AI Features

Setup JPA Repositories

Explore how to set up JPA repositories in Spring Data JPA. This lesson helps you understand repository types, annotations, and how to implement CRUD, pagination, and sorting features for efficient data access in your Spring applications.

What is a repository?

A repository is a class that provides storage, retrieval, and search behavior over a collection of objects that are usually persisted in the database.

Furthermore, simply by declaring the class with the @Repository annotation, we can define a repository in the Spring application, enabling autodetection through classpath scanning.

What is a JPA repository?

The JPA repository is the Repository interface provided by the Spring Data JPA over Java Persistence API (JPA). It provides a complete abstraction over the data access layer in a project.

First, the repository interfaces offer various abstract methods for storage, retrieval, and search features. Then, Spring provides proxy implementations for the defined interfaces at the runtime.

This reduces a lot of the boilerplate code required for creating an efficient data access layer such as the implementation and utilization of an entity manager.

The CrudRepository and PagingAndSortingRepository repositories are handy repository interfaces available in the generic Spring Data framework. However, Spring Data JPA also provides the JPA-specific JpaRepository.

Let’s explore these repositories a bit more before creating them:

  • The CrudRepository provides CRUD features through methods like save, findOne, findAll, count, exists, and delete.

  • The PagingAndSortingRepository extends the CrudRepository and provides pagination and sorting features over it.

  • The JpaRepository contains features of the PagingAndSortingRepository and CrudRepository, and extends it with JPA-related features like flushing and deleting in batches.

Create repositories in the todo app

The TodoTypeRepository for TodoType entity

For now, let’s create the TodoTypeRepository interface to provide DAO capabilities to the TodoType entity by extending Spring Data JPA’s CrudRepository. This will allow us to explore CRUD features.

Later, we’ll explore pagination and sorting capabilities by replacing the CrudRepository with the PagingAndSortingRepository.

We also annotate the PagingAndSortingRepository with the @Repository annotation, enabling Spring to identify them as repositories for the entities.

Java
package io.educative.repositories;
import io.educative.domains.TodoType;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.CrudRepository;
@Repository
public interface TodoTypeRepository extends CrudRepository<TodoType, String> {
}

Note that the type of the CrudRepository is defined as TodoType with String as the data type of the unique identifier variable code.

The TodoRepository for Todo entity

Similarly, we can create the TodoRepository by extending the CrudRepository with it’s type as the Todo entity and the Long data type for the unique identifier id.

Java
package io.educative.repositories;
import io.educative.domains.Todo;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.CrudRepository;
@Repository
public interface TodoRepository extends CrudRepository<Todo, Long> {
}

Once the repositories are ready, we can restart the todo application to enable the @SpringBootApplication annotation to automatically scan the repositories and add them to the classpath for injection in the service layer or other parts of the application.

The @EnableJpaRepository

Alternatively, we can use the @EnableJpaRepository to scan the repositories from the project or specific packages by using the basePackages parameter.