Spring Data Repositories

Spring Data Repositories

According to Martin Fowler, a repository mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.

In Spring Data JPA, repositories extend one of the Repository generic interfaces, including CrudRepository, ListCrudRepository, and JpaRepository.

repository interfaces hierarchy

To act on the data in a table, simply extend the interface of your choice by providing the entity type corresponding to the table and the type of its primary key.

1import org.springframework.data.jpa.repository.JpaRepository;
2
3public interface ProductRepository extends JpaRepository<Product, Long> {
4}

To define specific queries, we will mainly rely on the strategy described here.

1import org.springframework.data.jpa.repository.JpaRepository;
2
3import java.util.List;
4
5public interface OrderRepository extends JpaRepository<Order, Long> {
6
7    List<Order> findByOrderLinesQuantityGreaterThanEqual(int quantity);
8
9}