Bird
0
0

Examine the following Spring Boot repository class:

medium📝 Debug Q6 of 15
Spring Boot - Spring Annotations
Examine the following Spring Boot repository class:
@Repository
public interface ProductRepository {
    void addProduct(Product product);
}
What is the issue with this code?
AThe @Repository annotation cannot be used on interfaces
BInterfaces annotated with @Repository must extend a Spring Data interface like JpaRepository
CThe method addProduct must have a return type
DThe class should be annotated with @Service instead
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Repository usage

    The @Repository annotation is typically used on classes or interfaces that interact with the database.
  2. Step 2: Check interface requirements

    When using Spring Data JPA, repository interfaces should extend JpaRepository or a similar interface to inherit CRUD methods.
  3. Step 3: Analyze the given code

    The interface ProductRepository does not extend any Spring Data interface, so Spring cannot provide implementation automatically.
  4. Final Answer:

    Interfaces annotated with @Repository must extend a Spring Data interface like JpaRepository -> Option B
  5. Quick Check:

    Interface repositories need to extend JpaRepository [OK]
Quick Trick: Repository interfaces must extend JpaRepository or similar [OK]
Common Mistakes:
  • Assuming @Repository can be used on any interface without extending JpaRepository
  • Thinking @Repository is only for classes
  • Believing method return types are mandatory for repository methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes