Repository not necessary when implementing JpaRepository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you create an interface that extends JpaRepository, you do not need to add the @Repository annotation. Spring Data JPA automatically detects interfaces that extend its repository base interfaces (Repository, CrudRepository, JpaRepository) and creates proxy implementations at startup. The @Repository annotation is technically redundant because SimpleJpaRepository — the default implementation Spring generates — is already annotated with @Repository internally.
Why @Repository Is Not Needed
Spring Boot auto-configuration scans for interfaces extending JpaRepository through @EnableJpaRepositories, which is included automatically via @SpringBootApplication. The scanning mechanism does not rely on @Repository — it looks for the JpaRepository (or CrudRepository, Repository) type hierarchy.
How Spring Detects Repositories
The auto-configuration chain works like this: @SpringBootApplication enables @EnableAutoConfiguration, which activates JpaRepositoriesAutoConfiguration, which internally calls @EnableJpaRepositories with the application's base package. Any interface extending Spring Data's Repository marker interface (which JpaRepository does) gets a generated proxy.
What @Repository Actually Does
@Repository serves two purposes in Spring:
- It is a
@Componentspecialization, making the class eligible for component scanning and bean creation. - It enables automatic translation of persistence exceptions (e.g., converting
SQLExceptionto Spring'sDataAccessExceptionhierarchy).
For JpaRepository interfaces, both functions are already handled: Spring Data creates the bean via its own mechanism, and SimpleJpaRepository (the generated implementation) already has @Repository applied.
When You Might Still Use @Repository
When you write a concrete class that directly uses EntityManager or JDBC without extending a Spring Data interface, @Repository is needed for exception translation and component scanning.
Custom Base Package Scanning
By default, @EnableJpaRepositories scans the package of the main application class and its sub-packages. If your repository interfaces are in a different package tree, you must specify basePackages explicitly. Even in this case, @Repository is not needed on the interfaces.
The Repository Hierarchy
Any interface extending any level of this hierarchy is auto-detected. You choose which level based on what methods you need.
Exception Translation Without @Repository
The proxy created by Spring Data wraps calls in a PersistenceExceptionTranslationInterceptor, which translates JPA exceptions to Spring's DataAccessException hierarchy. This happens regardless of whether you add @Repository to the interface.
Common Pitfalls
- Adding @Repository out of habit: While harmless, it creates the false impression that the annotation is required. It clutters the code with unnecessary boilerplate. Omit it for Spring Data interfaces.
- Repository interface not in scanned package: If the interface is outside the base package of
@SpringBootApplication, Spring does not find it. Use@EnableJpaRepositories(basePackages = "...")to specify the correct package. This is a scanning issue, not a@Repositoryissue. - Confusing @Repository with @Service or @Component: All three are Spring stereotypes, but
@Repositoryspecifically adds persistence exception translation. Using@Serviceon a repository class loses that behavior. - Custom implementations missing @Repository: When you write a concrete
*Implclass for custom repository logic usingEntityManagerdirectly, you do need@Repositoryfor exception translation. The Spring Data proxy only covers the generated methods. - Multiple @EnableJpaRepositories conflicting: If you have multiple
@EnableJpaRepositoriesannotations with overlapping packages, Spring may create duplicate beans. Keep repository scanning configuration in one place.
Summary
@Repositoryis not needed on interfaces extendingJpaRepository,CrudRepository, orRepository- Spring Data JPA auto-detects repository interfaces through
@EnableJpaRepositories(triggered by@SpringBootApplication) SimpleJpaRepository(the generated proxy) already carries@Repositoryinternally- Exception translation is handled by the proxy regardless of the annotation
- Use
@Repositoryonly on concrete classes that interact withEntityManageror JDBC directly - If repositories are not detected, check package scanning configuration, not
@Repositorypresence

