Which of the following annotations is used to enable JPA repositories in Spring Boot?

Which of the following annotations is used to enable JPA repositories in Spring Boot?

A) @EnableJpaRepositories
B) @EnableAutoConfiguration
C) @EnableTransactionManagement
D) @EnableJpaAuditing

Answer:

A) @EnableJpaRepositories

Explanation:

The @EnableJpaRepositories annotation is used in Spring Boot to enable the scanning of JPA repositories, which are interfaces that extend the JpaRepository interface to provide CRUD operations and custom queries for your entities. By adding this annotation, Spring Boot will automatically scan the package for JPA repository interfaces and create proxy implementations for them at runtime.

For example:


@SpringBootApplication
@EnableJpaRepositories(basePackages = "com.example.repositories")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

In this example, the @EnableJpaRepositories annotation is used to scan the com.example.repositories package for JPA repository interfaces. This setup is essential for enabling the JPA-based data access layer in a Spring Boot application, allowing developers to interact with the database using repository interfaces without writing boilerplate code for data access.

Reference links:

Spring Boot Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top