Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In Spring Framework, the @Repository annotation plays a crucial role in defining data access objects (DAOs). It is a specialized component of the Spring stereotype annotations, primarily used to indicate that a class interacts with a database. By marking a class with @Repository, Spring enables automatic exception translation, converting database-related exceptions into Spring’s DataAccessException, making error handling more manageable.
What is @Repository in Spring Boot?
Spring has various annotations, and one such annotation is @component. The specialization of annotation @component is @repository. The @repository annotation is built on the top of the classes that manage the code related to the database. The annotation helps to easily retrieve, update, and delete content in the database.
Also, by using the annotation @component, you create beans with lesser codes. Repository annotation helps to find the repository class. The repository class is the class that represents the database. You write this annotation just above the class used for the database. This annotation is similar to the DAO (Direct Access Object) class responsible for CRUD operation.
Spring Repository Example
@Repository
public class CustomerRepositoryImpl implements CustomerRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Customer> getAllCustomers() {
String sql = "SELECT * FROM customers";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Customer.class));
}
// other methods for customer repository
}
In this example, the CustomerRepositoryImpl class is annotated with @Repository to indicate that it is a repository component in the Spring application. The class implements the CustomerRepository interface and uses JdbcTemplate to query the database and return a list of Customer objects. The @Autowired annotation is used to inject the JdbcTemplate instance into the class.
The layout of the application will look like this:
We have created a maven project named Annotations in STS. We have two packages inside the Annotations.
Entity package: Inside this, we have an employee class.
Repository package: Inside this, we have one class and one interface, EmployeeDao as a class and an interface GenRepository.
When you explore the maven dependency, you can see that the spring-context dependency is added.
Employee.java
package com.CN.Annotations.entity;
public class Employee {
private int id;
private String name;
public Employee(int id, String name) {
this.id= id;
this.name= name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id =id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return id +" ," + name;
}
public void printEmployee(int id, String name) {
System.out.println("Employee id is "+ id +" and name is: " +name);
}
}
GenRepository.java
This is the generic class. Here T is generic to the object we refer to. We have three methods in this interface. These methods are overridden in the class that implements this interface.
package com.CN.Annotations.Repository;
public interface GenRepository<T> {
public void save(T t);
public T findEmployeeById(int id);
public T deleteEmployee(int id);
}
The EmployeeDao class is the repository class. This class helps in retrieving, deleting, and updating the data. This class implements the GenRepository interface and overrides the method of the interface.
package com.CN.Annotations.Repository;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.CN.Annotations.entity.Employee;
@Repository
public class EmployeeDao implements GenRepository<Employee> {
private Map <Integer, Employee> dao;
public EmployeeDao() {
this.dao= new HashMap<>();
}
@Override
public void save(Employee employee) {
dao.put(employee.getId(), employee);
}
@Override
public Employee findEmployeeById(int id) {
return dao.get(id);
}
@Override
public Employee deleteEmployee(int id) {
Employee emp= dao.get(id);
this.dao.remove(emp);
return emp;
}
}
App.java
The program begins from app.java file. Through this, we have tried to add the data to the repository class. The dao object we get is through the context object we obtain by AnnotationConfigApplicationContext.
The data is then printed by getting the data from the findEmployeeById method.
Below is the output when we run the application as a java application.
Features of Repository Annotation
Repository annotation lets you showcase the class that provides connectivity with the database. You can modify the data through the @repository annotation class. Different features of Repository Annotation are as follows:
Retrieve data from the table
Save data to the table
Update data in the table
Delete data from the data
Frequently Asked Questions
What is repository annotation?
The @Repository annotation in Spring is used to indicate that a particular class is a repository component in a Spring application. It is used to enable automatic exception translation and provides access to data persistence and retrieval mechanisms.
Is @repository annotation necessary?
No, the @Repository annotation is not strictly necessary for a Spring application to function, but it is considered a good practice to use it to indicate that a particular class is a repository component and to benefit from Spring's automatic exception translation for data access exceptions.
What is @repository in Java?
In Java, "@Repository" is an annotation used to indicate that a class is a repository, which is a component responsible for accessing and manipulating data from a database. It is typically used in conjunction with other annotations, such as "@Autowired" and "@Transactional", to provide additional functionality.
What is the purpose of repository in Spring boot?
In Spring Boot, a repository is used to manage data persistence and retrieval in a database. It provides an abstraction layer over the database and simplifies the process of interacting with it, allowing developers to focus on business logic rather than database operations.
Is @repository mandatory in Spring Boot?
No, @Repository is not mandatory in Spring Boot. Spring Data JPA automatically detects repository interfaces extending JpaRepository, but @Repository enables exception translation for better error handling.
Conclusion
In this blog, we have discussed repository annotations. We looked at the features and implementation of the annotation. We code a spring project using the annotation for better understanding.
To learn more about Spring, please refer to the blogs given below: