Repository Pattern vs DAO Pattern in Spring Data
Fast recall
Quick revision
Core answer
Understanding of DDD concepts and the practical difference in abstraction level and layer ownership
Treating DAO and Repository as synonyms, or putting complex business logic inside a Repository/DAO
Explain that in DDD, a Repository deals with Aggregate Roots and hides persistence completely, whereas DAO exposes CRUD operations and often leaks persistence concepts like pagination SQL
Interview-ready guide
Interview answer
DAO and Repository are often used interchangeably, but they come from different design traditions with different implications. DAO is a J2EE pattern focused on abstracting database access — a UserDao wraps SQL operations for the users table, exposing findById, save, delete. It lives in the infrastructure layer and is allowed to know about database concepts like result sets and entity managers.
Repository comes from Domain-Driven Design. It models a collection of Aggregate Root objects. From the domain's perspective, the Repository looks like an in-memory collection — you add objects, query them, remove them, and persistence details are invisible. The interface lives in the domain layer and speaks domain language: findActiveOrdersForCustomer, not SELECT ... WHERE status = 'ACTIVE'.
Spring Data's JpaRepository has blurred this distinction. It's named Repository following DDD convention but behaves like a DAO — it generates SQL, manages EntityManager, and exposes Pageable and flush(). In a rigorous DDD application, you'd define a pure domain OrderRepository interface with zero Spring imports, and provide a JpaOrderRepository implementation in the infrastructure layer that delegates to Spring Data internally.
For most Spring Boot projects, using JpaRepository directly is pragmatic and correct. The domain/infrastructure split only earns its cost when the domain needs to be persistence-ignorant — complex business rules, hexagonal architecture, or realistic plans to switch databases. My default: start with JpaRepository, extract a domain-pure interface when the architecture demands it.
Repository Pattern vs DAO Pattern in Spring Data
Direct Answer
DAO (Data Access Object) is an infrastructure pattern that abstracts database operations for a specific table. Repository is a domain-driven design concept — it abstracts a collection of domain objects and belongs to the domain layer. Spring Data's JpaRepository is physically a DAO but named Repository following DDD convention.
Common Mistakes & Pitfalls
Treating DAO and Repository as synonyms, or putting complex business logic inside a Repository/DAO
Check your understanding
Answer these without looking back at the article.
- How would you implement a Repository interface that is independent of Spring Data?
- What is an Aggregate Root in DDD and why does Repository operate at that level?
- When would you use JdbcTemplate over JpaRepository?
Related interview prep categories
2Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.