What is Spring Data JPA and how do repository interfaces work?
Learn what Spring Data JPA is, how repository interfaces generate CRUD and derived queries at runtime, when to use @Query, with examples and interview tips.
Expected Interview Answer
Spring Data JPA is an abstraction over JPA that eliminates boilerplate data-access code by letting you declare repository interfaces; Spring generates the implementation at runtime, providing CRUD, paging, and derived query methods without you writing any SQL or JPQL.
You define an interface extending JpaRepository<Entity, ID> (or CrudRepository/PagingAndSortingRepository). At startup Spring Data creates a proxy implementation backed by an EntityManager. Standard methods like save, findById, findAll, and deleteById come for free. You add custom finders by following the derived-query naming convention (findByEmailAndActiveTrue), which Spring parses into a query, or you write explicit JPQL/SQL with @Query. This keeps the persistence layer declarative and drastically reduces DAO boilerplate.
- Eliminates hand-written DAO and boilerplate CRUD code
- Derived query methods generate queries from method names
- Built-in paging and sorting support
- @Query allows custom JPQL or native SQL when needed
- Consistent, testable repository abstraction across the codebase
AI Mentor Explanation
Spring Data JPA is like a team's support staff who prepare everything from a simple instruction: you just say 'get me the fast bowlers who took wickets today' and the analysts fetch the exact players, without you personally trawling the entire scorebook line by line.
Step-by-Step Explanation
Step 1
Define the entity
Annotate a class with @Entity and @Id so JPA maps it to a table.
Step 2
Declare the repository
Create an interface extending JpaRepository<Entity, IdType>; no implementation needed.
Step 3
Let Spring build the proxy
At startup Spring Data generates a proxy backed by an EntityManager implementing your interface.
Step 4
Use built-in methods
Call save, findById, findAll, deleteById directly — inherited CRUD comes for free.
Step 5
Add custom queries
Write derived finders like findByLastName, or annotate a method with @Query for JPQL/native SQL.
What Interviewer Expects
- Knowing Spring Data JPA sits on top of JPA/Hibernate
- Understanding that repository implementations are generated at runtime
- Explaining derived query method naming conventions
- When to fall back to @Query for JPQL or native SQL
- Awareness of the CrudRepository/JpaRepository hierarchy and paging support
Common Mistakes
- Thinking you must write the repository implementation yourself
- Confusing Spring Data JPA with JPA or Hibernate itself
- Writing invalid derived method names that fail to parse at startup
- Ignoring the N+1 query problem when fetching associations
- Using native queries where a portable JPQL query would suffice
Best Answer (HR Friendly)
“Spring Data JPA is a tool that saves developers from writing repetitive database code. You simply declare an interface describing what data you want, and Spring automatically creates the code that talks to the database, including ready-made methods to save, find, and delete records.”
Code Example
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String email;
private boolean active;
}
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByActiveTrue();
Optional<User> findByEmail(String email);
}public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.email LIKE %:domain%")
List<User> findByEmailDomain(@Param("domain") String domain);
}Follow-up Questions
- What is the difference between CrudRepository and JpaRepository?
- How does Spring Data parse a derived query method name?
- When would you use @Query with a native query?
- How do you implement pagination and sorting?
- What is the N+1 select problem and how do you avoid it?
MCQ Practice
1. Who provides the implementation of a Spring Data JPA repository interface?
Spring Data creates a proxy implementation of the interface at startup, backed by an EntityManager.
2. Which method name would correctly derive a query for active users by last name?
Derived queries follow the findBy<Property> convention, combining conditions with And/Or and keywords like True.
3. When is @Query typically used?
@Query lets you write explicit JPQL or native SQL when the method-name derivation cannot express the query.
Flash Cards
What is Spring Data JPA? — An abstraction over JPA that generates repository implementations from interfaces, removing boilerplate data-access code.
How are repository methods implemented? — Spring Data creates a runtime proxy backed by an EntityManager; you never write the implementation.
What is a derived query method? — A finder whose name (e.g. findByEmailAndActiveTrue) is parsed by Spring into a query.
When do you use @Query? — When a query is too complex for method-name derivation, to supply explicit JPQL or native SQL.
Continue Learning
Related Interview Questions
What is the difference between JpaRepository, CrudRepository, and PagingAndSortingRepository?
medium
What is the N+1 query problem in Spring Data JPA and how do you solve it?
hard
How does Spring Boot handle application configuration with application.properties and profiles?
medium
What are Spring bean scopes and how do singleton and prototype differ?
medium