Skip to main content


ArchitecturalData accessAbout 2 min


Repository layer is added between the domain and data mapping layers to isolate domain objects from
details of the database access code and to minimize scattering and duplication of query code. The
Repository pattern is especially useful in systems where number of domain classes is large or heavy
querying is utilized.


Real world example

Let's say we need a persistent data store for persons. Adding new persons and searching for them
according to different criteria must be easy.

In plain words

Repository architectural pattern creates a uniform layer of data repositories that can be used for
CRUD operations.

Microsoft documentationopen in new window says

Repositories are classes or components that encapsulate the logic required to access data sources.
They centralize common data access functionality, providing better maintainability and decoupling
the infrastructure or technology used to access databases from the domain model layer.

Programmatic Example

Let's first look at the person entity that we need to persist.

public class Person {

  private Long id;
  private String name;
  private String surname;
  private int age;

   * Constructor.
  public Person(String name, String surname, int age) { = name;
    this.surname = surname;
    this.age = age;


We are using Spring Data to create the PersonRepository so it becomes really simple.

public interface PersonRepository
    extends CrudRepository<Person, Long>, JpaSpecificationExecutor<Person> {

  Person findByName(String name);

Additionally we define a helper class PersonSpecifications for specification queries.

public class PersonSpecifications {

  public static class AgeBetweenSpec implements Specification<Person> {

    private final int from;

    private final int to;

    public AgeBetweenSpec(int from, int to) {
      this.from = from; = to;

    public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
      return cb.between(root.get("age"), from, to);


  public static class NameEqualSpec implements Specification<Person> {

    public String name;

    public NameEqualSpec(String name) { = name;

    public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
      return cb.equal(root.get("name"),;


And here's the repository example in action.

    var peter = new Person("Peter", "Sagan", 17);
    var nasta = new Person("Nasta", "Kuzminova", 25);
    var john = new Person("John", "lawrence", 35);
    var terry = new Person("Terry", "Law", 36);;;;;"Count Person records: {}", repository.count());

    var persons = (List<Person>) repository.findAll();;


    repository.findById(2L).ifPresent(p ->"Find by id 2: {}", p));
    repository.deleteById(2L);"Count Person records: {}", repository.count());

        .findOne(new PersonSpecifications.NameEqualSpec("John"))
        .ifPresent(p ->"Find by John is {}", p));

    persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));"Find Person with age between 20,40: ");;


Program output:

Count Person records: 4
Person [id=1, name=Peter, surname=Sagan, age=17]
Person [id=2, name=Nasta, surname=Kuzminova, age=25]
Person [id=3, name=John, surname=lawrence, age=35]
Person [id=4, name=Terry, surname=Law, age=36]
Find by id 2: Person [id=2, name=Barbora, surname=Spotakova, age=25]
Count Person records: 3
Find by John is Person [id=3, name=John, surname=lawrence, age=35]
Find Person with age between 20,40: 
Person [id=3, name=John, surname=lawrence, age=35]
Person [id=4, name=Terry, surname=Law, age=36]

Class diagram

alt text


Use the Repository pattern when

  • The number of domain objects is large.
  • You want to avoid duplication of query code.
  • You want to keep the database querying code in single place.
  • You have multiple data sources.

Real world examples