Spring Data JPA Repositories Interview Questions and Answers

Master Spring Data JPA Repositories with interview questions covering Repository, CrudRepository, PagingAndSortingRepository, JpaRepository, custom repositories, repository hierarchy, proxy generation, and production best practices.


Introduction

Repositories are one of the most powerful features of Spring Data JPA.

Before Spring Data JPA, developers had to write repetitive DAO implementations using EntityManager.

Example

entityManager.persist()

entityManager.merge()

entityManager.remove()

entityManager.find()

Spring Data JPA eliminates this boilerplate by generating repository implementations automatically at runtime.

Developers simply define interfaces, and Spring creates the implementation.


Repository Architecture

flowchart LR

Application --> Repository

Repository --> Hibernate

Hibernate --> Database

Q1. What is a Repository in Spring Data JPA?

Answer

A Repository is a Spring-managed interface that abstracts database access.

It provides methods for

  • CRUD operations
  • Query execution
  • Pagination
  • Sorting
  • Batch operations

Example

public interface

CustomerRepository

extends JpaRepository<

Customer,

Long>{

}

Spring automatically generates the implementation.


Q2. What is the Repository hierarchy?

Spring Data JPA provides multiple repository interfaces.

Repository

↓

CrudRepository

↓

PagingAndSortingRepository

↓

JpaRepository

Repository Hierarchy

flowchart TD

Repository --> CrudRepository

CrudRepository --> PagingAndSortingRepository

PagingAndSortingRepository --> JpaRepository

Each interface adds additional capabilities.


Q3. What is Repository?

Repository is a marker interface.

Example

public interface

Repository<T,ID>{

}

Purpose

  • Identifies repository interfaces.
  • Enables Spring Data infrastructure.

It does not provide CRUD methods.


Q4. What is CrudRepository?

CrudRepository provides basic CRUD operations.

Common methods

save()

findById()

findAll()

delete()

existsById()

count()

Suitable for applications requiring only basic persistence operations.


Q5. What is PagingAndSortingRepository?

It extends CrudRepository.

Additional methods

findAll(

Pageable pageable)

findAll(

Sort sort)

Benefits

  • Pagination
  • Sorting
  • Efficient retrieval of large datasets

Pagination Flow

flowchart LR

Repository --> Pageable

Pageable --> Database

Database --> Page

Q6. What is JpaRepository?

JpaRepository extends PagingAndSortingRepository.

Additional features

  • Batch operations
  • Flush support
  • Lazy reference retrieval
  • Advanced JPA functionality

Common methods

saveAndFlush()

flush()

deleteAllInBatch()

getReferenceById()

JpaRepository is the most commonly used repository interface.


Q7. Which Repository should we use?

Repository Use Case
Repository Marker interface
CrudRepository Basic CRUD
PagingAndSortingRepository CRUD + Pagination
JpaRepository Enterprise applications

For most Spring Boot projects,

JpaRepository is the recommended choice.


Q8. How does Spring generate Repository implementations?

Developers define only interfaces.

Example

public interface

PaymentRepository

extends JpaRepository<

Payment,

Long>{

}

During startup

  • Spring scans repository interfaces.
  • Creates proxy implementations.
  • Delegates operations to Hibernate.

Repository Proxy

flowchart LR

RepositoryInterface --> SpringProxy

SpringProxy --> Hibernate

Hibernate --> Database

No manual implementation is required.


Q9. Can we create Custom Repository methods?

Yes.

Example

List<Customer>

findByCity(

String city);

Or

@Query(

"select c from Customer c")

Spring automatically executes the query.

Complex business logic can also be implemented using custom repository implementations.


Q10. Repository Best Practices

Prefer JpaRepository

Provides the richest feature set.


Keep Repositories Focused

One repository per aggregate/entity.


Avoid Business Logic

Repositories should only perform persistence operations.


Use Pagination

Avoid loading millions of records into memory.


Return Optional

Use Optional<T> for single-record lookups instead of returning null.


Banking Example

flowchart TD

CustomerController --> CustomerService

CustomerService --> CustomerRepository

CustomerRepository --> Hibernate

Hibernate --> PostgreSQL

CustomerRepository --> JpaRepository

Business logic remains in the service layer while repositories focus solely on data access.


Common Interview Questions

  • What is a Repository?
  • Repository hierarchy?
  • What is CrudRepository?
  • What is PagingAndSortingRepository?
  • What is JpaRepository?
  • Which repository should we use?
  • How are repository implementations generated?
  • What methods does JpaRepository provide?
  • Can we create custom repository methods?
  • Repository best practices?

Quick Revision

Repository Features
Repository Marker interface
CrudRepository CRUD operations
PagingAndSortingRepository CRUD + Pagination + Sorting
JpaRepository Full JPA functionality
save() Insert or update
findById() Retrieve entity
delete() Remove entity
flush() Synchronize persistence context
saveAndFlush() Save immediately
deleteAllInBatch() Batch delete

Repository Execution Flow

sequenceDiagram
Application->>Repository Interface: save(entity)
Repository Interface->>Spring Proxy: Delegate
Spring Proxy->>Hibernate: Persist Entity
Hibernate->>Database: INSERT
Database-->>Hibernate: Success
Hibernate-->>Spring Proxy: Entity Saved
Spring Proxy-->>Application: Response

Production Example – Banking Account Management

A banking platform manages customer accounts.

Repository Structure

  • CustomerRepository extends JpaRepository<Customer, Long>.
  • AccountRepository extends JpaRepository<Account, Long>.
  • TransactionRepository extends JpaRepository<Transaction, Long>.

Operations

  • CustomerRepository performs customer CRUD operations.
  • AccountRepository supports pagination for account listings.
  • TransactionRepository retrieves transactions using derived query methods and custom JPQL queries.
  • Batch updates use saveAll() and deleteAllInBatch() for better performance.
flowchart LR

CustomerService --> CustomerRepository

AccountService --> AccountRepository

TransactionService --> TransactionRepository

Repositories --> SpringProxy["Spring Proxy"]

SpringProxy["Spring Proxy"] --> Hibernate

Hibernate --> PostgreSQL

This repository-based architecture minimizes boilerplate code, improves readability, and provides a clean separation between business logic and persistence logic.


Key Takeaways

  • Repositories provide a high-level abstraction for database access in Spring Data JPA.
  • The repository hierarchy progresses from Repository → CrudRepository → PagingAndSortingRepository → JpaRepository, with each level adding more functionality.
  • JpaRepository is the preferred choice for most enterprise applications because it includes CRUD operations, pagination, sorting, flushing, and batch processing.
  • Spring automatically generates repository implementations at runtime using dynamic proxies.
  • Repository interfaces should remain focused on persistence concerns, while business logic belongs in the service layer.
  • Use pagination for large datasets and return Optional<T> for single-entity retrieval to write safer code.
  • Custom query methods and JPQL queries allow repositories to support complex retrieval requirements without manual DAO implementations.
  • Repository abstraction significantly reduces boilerplate code and improves productivity in modern Spring Boot applications.