Bird
0
0

What is wrong with this Spring Boot repository class?

medium📝 Debug Q14 of 15
Spring Boot - Spring Annotations
What is wrong with this Spring Boot repository class?
@Repository
public class UserRepository {
    public void saveUser(String user) {
        // save logic
    }
}
Options: A) Class should be an interface extending JpaRepository B) Missing @Service annotation C) @Repository cannot be used on classes D) Method saveUser must return a value
AClass should be an interface extending JpaRepository
BMissing @Service annotation
C@Repository cannot be used on classes
DMethod saveUser must return a value
Step-by-Step Solution
Solution:
  1. Step 1: Understand typical repository pattern in Spring Boot

    Spring Data JPA repositories are usually interfaces extending JpaRepository or CrudRepository to get built-in methods.
  2. Step 2: Identify the issue with the class declaration

    The class is a concrete class, not an interface extending JpaRepository, so it misses Spring Data JPA features.
  3. Final Answer:

    Class should be an interface extending JpaRepository -> Option A
  4. Quick Check:

    Repositories usually extend JpaRepository interface [OK]
Quick Trick: Repositories are interfaces extending JpaRepository, not plain classes [OK]
Common Mistakes:
  • Thinking @Repository cannot be on classes
  • Assuming @Service is needed for repositories
  • Believing saveUser must return a value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes