The @Repository annotation is used in the persistence layer of a Spring application to interact with the database. Classes marked with @Repository contain the code required to perform CRUD operations and other database-related tasks. It acts as a bridge between the business layer and the database layer, helping maintain a clean separation of responsibilities within the application.
- Provides automatic translation of database-specific exceptions into Spring's DataAccessException hierarchy.
- Works seamlessly with Spring Data JPA, Hibernate, JDBC, and other persistence technologies.
- Enables dependency injection so repository objects can be used in service classes.
- Improves code organization by keeping database logic in a dedicated layer.
- Supports easier testing and maintenance through proper layer separation.
Use Cases of @Repository
- Saving Data into a Database: Used to insert records such as employees, students, customers, or products.
- Retrieving Data from a Database: Fetching all records or a specific record based on an identifier.
- Updating Existing Records: Modifying employee details, customer information, or product data.
- Deleting Records: Removing unwanted or outdated data from the database.
- Custom Database Queries: Executing JPQL, HQL, native SQL, or derived query methods to retrieve filtered data.
- Implementing Data Access Layer (DAO): Centralizing all database interaction logic in a dedicated repository layer.
Steps to Implements the @Repository Annotation
Let's consider a simple example to understand how to use the @Repository annotation in a Spring Boot application.
Step 1: Create a Simple Spring Boot Project
Refer to this article Create and Setup Spring Boot Project in Eclipse IDE and create a simple spring boot project.
Step 2: Add the Spring-Context Dependency
In your pom.xml file, add the following spring-context dependency.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
Step 3: Create Packages and Classes
Create two packages: entity and repository. Inside the entity package, create a Student class. In the repository package, create a generic interface named DemoRepository and a class named StudentRepository.

Step 4: Create the Entity Class
Create an entity class for which we will implement a spring repository. Here our entity class is Student. Below is the code for the Student.java file. This is a simple POJO (Plain Old Java Object) class in Java.
Student.java:
package com.example.demo.entity;
public class Student {
private Long id;
private String name;
private int age;
public Student(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
Step 5: Create the Repository Interface
Before implementing the Repository class we have created a generic DemoRepository interface to define the contract for our repository class. Below is the code for the DemoRepository.java file.
package com.example.demo.repository;
public interface DemoRepository<T> {
// Save method
public void save(T t);
// Find a student by its id
public T findStudentById(Long id);
}
Step 6: Implement the Repository Class
Now let’s look at the implementation of our StudentRepository class.
package com.example.demo.repository;
import com.example.demo.entity.Student;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.Map;
@Repository
public class StudentRepository implements DemoRepository<Student> {
// Using an in-memory Map
// to store the object data
private Map<Long, Student> repository;
public StudentRepository() {
this.repository = new HashMap<>();
}
// Implementation for save method
@Override
public void save(Student student) {
repository.put(student.getId(), student);
}
// Implementation for findStudentById method
@Override
public Student findStudentById(Long id) {
return repository.get(id);
}
}
In this StudentRepository.java file, you can notice that we have added the @Repository annotation to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects.

Note: Here we have used an in-memory Map to store the object data, you can use any other mechanisms too. In the real world, we use Databases to store object data.
Step 7: Spring Repository Test
Now our Spring Repository is ready, let’s test it out. Go to the DemoApplication.java file and refer to the below code.
package com.example.demo;
import com.example.demo.entity.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.example.demo");
context.refresh();
StudentRepository repository = context.getBean(StudentRepository.class);
// testing the store method
repository.save(new Student(1L, "Anshul", 25));
repository.save(new Student(2L, "Mayank", 23));
// testing the retrieve method
Student student = repository.findStudentById(1L);
System.out.println(student);
// close the spring context
context.close();
}
}
Step 8: Run the Application
- Open DemoApplication.java.
- Right-click ->Run As ->Spring Boot App.
Output:
