DEV Community

Willian Ferreira Moya
Willian Ferreira Moya

Posted on • Originally published at springmasteryhub.com

Understanding @Repository in Spring

What is @Repository?

This is a class-level annotation that indicates that the Class's purpose is to store, search, retrieve, update, and delete objects. It has a special role when dealing with databases.

Why is it important?

  • Legibility: This annotation separates the communication with the database in a separate layer.
  • Simplifies database communication: You can define an interface that extends JpaRepositoryCrudRepository, or PagingAndSortingRepository and save some time when implementing basic interactions with the database.
  • Translate exceptions: It will convert database exceptions into Spring's DataAccessException hierarchy, converting the JPA exceptions into a more consistent exception model that Spring provides.

How to use @Repository

Define an interface, annotate with @Repository, and extend one of those interfaces:

JpaRepositoryCrudRepository, or PagingAndSortingRepository.

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

Enter fullscreen mode Exit fullscreen mode

This will already set you up with some basic main operations. If you need, you can add some custom methods like this:

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findByUsername(String username);
    List<User> findAllByActiveTrue();
}

Enter fullscreen mode Exit fullscreen mode

Bellow you have more examples using the other interfaces that I’ve mentioned

JpaRepository

package org.spring.mastery.repository;

import org.spring.mastery.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserJpaRepository extends JpaRepository<User, Long> {
    Optional<User> findByUsername(String username);
    List<User> findAllByActiveTrue();
}
Enter fullscreen mode Exit fullscreen mode

CrudRepository

package org.spring.mastery.repository;

import org.spring.mastery.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserCrudRepository extends CrudRepository<User, Long> {
    Optional<User> findByUsername(String username);
    List<User> findAllByActiveTrue();
}
Enter fullscreen mode Exit fullscreen mode

Dependencies

To use Spring Data JPA and H2, you need to add the following dependencies to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Conclusion

By using @Repository, you can utilize the advantages and facilities that Spring offers when working with databases. You’ll be set up fast to deliver value with your application, avoiding the boilerplate necessary to communicate with a database.

Top comments (0)