In a Spring Boot application, the repository layer interacts with the database using Spring Data JPA. Below is a structured overview of key annotations used in this layer, categorized by their scope and purpose:
Type-Level Annotations (Applied on the Repository Interface)
@Repository
Purpose: Marks the interface as a Spring repository bean. While optional (Spring automatically detects repositories), it enables exception translation (e.g., converting JDBC exceptions to Spring's DataAccessException hierarchy).
Example:
java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
@NoRepositoryBean
Purpose: Prevents Spring from creating a bean for an abstract or base repository interface.
Example:
java
@NoRepositoryBean
public interface CustomRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {}
@EnableJpaRepositories
Purpose: Configuration annotation (used in a @Configuration class) to enable JPA repositories. Specifies package paths for repository scanning.
Example:
java
@Configuration
@EnableJpaRepositories("com.example.repository")
public class JpaConfig {}
Method-Level Annotations (Applied on Repository Methods)
@Query
Purpose: Defines a custom JPQL or SQL query for a repository method.
Example:
java
@Query("SELECT u FROM User u WHERE u.email = ?1")
User findByEmail(String email);
@Modifying
Purpose: Marks a query as an update/delete operation (used with @Query). Requires @Transactional if used outside a transactional context.
Example:
java
@Modifying
@Query("UPDATE User u SET u.active = false WHERE u.id = ?1")
void deactivateUser(Long id);
@Param
Purpose: Names method parameters for use in @Query expressions, improving readability.
Example:
java
@Query("SELECT u FROM User u WHERE u.firstName = :name")
List<User> findByFirstName(@Param("name") String firstName);
@Lock
Purpose: Specifies a lock mode (e.g., PESSIMISTIC_WRITE, OPTIMISTIC) for queries to handle concurrency.
Example:
java
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT u FROM User u WHERE u.id = ?1")
User findWithLock(Long id);
@EntityGraph
Purpose: Defines an entity graph to optimize fetching related entities (e.g., avoiding N+1 queries).
Example:
java
@EntityGraph(attributePaths = {"orders", "address"})
User findEagerUserById(Long id);
@Procedure
Purpose: Invokes a stored procedure or function defined in the database.
Derived Queries: No annotations needed. Method names like findByLastName(String lastName) are parsed automatically.
Custom Implementations: For custom logic, create a class with a method matching the repository interface's method name. Spring Data JPA merges the interface and implementation.
Exception Translation: Use @Repository on the interface to enable Spring's DataAccessException handling.
Example Repository Interface
java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.email = :email")
User findByEmail(@Param("email") String email);
@Modifying
@Transactional
@Query("UPDATE User u SET u.active = false WHERE u.id = ?1")
void deactivateUser(Long id);
@Lock(LockModeType.READ)
@Query("SELECT u FROM User u WHERE u.id = ?1")
User findWithReadLock(Long id);
}
By leveraging these annotations, you can customize query behavior, manage transactions, and optimize database interactions in the repository layer of a Spring Boot application.