Quick revision
The short answer
A DDD repository gives collection-like access to whole aggregates: find one by id, add it, remove it, and run a few queries phrased in the domain's language, with one repository per aggregate root. A DAO is usually table-oriented and exposes persistence operations for any table, including child tables. Spring Data generates repository implementations from interfaces, and those interfaces serve well as DDD repositories when we create them only for aggregate roots; the interfaces belong to the domain and the implementations to the infrastructure.
What to remember
In this answer, we saw that a DDD repository gives collection-like access to aggregates, with find by id, add, remove and domain-specific queries, and that there is one repository per aggregate root, not per table or child entity. A DAO is usually table-oriented and exposes persistence operations. Spring Data generates repository implementations from interfaces, and those serve as DDD repositories when we limit them to aggregate roots and expose only the methods we need. The interfaces belong to the domain, while the implementations belong to the infrastructure.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A DDD repository gives collection-like access to whole aggregates: find one by id, add it, remove it, and run a few queries phrased in the domain's language, with one repository per aggregate root. A DAO is usually table-oriented and exposes persistence operations for any table, including child tables. Spring Data generates repository implementations from interfaces, and those interfaces serve well as DDD repositories when we create them only for aggregate roots; the interfaces belong to the domain and the implementations to the infrastructure.
Why this question is asked
Most Spring developers have written hundreds of interfaces that extend JpaRepository, so it is easy to think a repository is just a Spring Data interface. Interviewers ask this question to see whether we know the idea behind the name, because it decides how many repositories we create, what methods they offer and where they live in the code. In this answer, we will recap what a DAO is, see what a DDD repository adds, look at the one-repository-per-root rule, see how Spring Data fits, and place the interface and the implementation in the right layers.
1A Quick Recap of the DAO
A Data Access Object, or DAO, is an older pattern from Java enterprise applications. Its job is to hide the details of the database behind a class, so the rest of the code does not write SQL. A DAO is usually shaped around a table: OrderDao, OrderLineDao, AddressDao, each with insert, update, delete and findBy methods that mirror the table's columns.
That is useful, but notice what it exposes: persistence operations. The caller decides when to update which table, and nothing stops a service from updating order_line rows directly without going through the order. The DAO knows about the database; it knows nothing about the business rules that tie rows together.
2What a DDD Repository Adds
In DDD, a repository pretends to be an in-memory collection of aggregates. We can ask it for an aggregate by id, add a new aggregate, remove one, and run a few queries that the domain needs, named in the business's language. It hands back whole aggregates, fully able to enforce their rules, and it takes whole aggregates back to store them.
The reason for this shape is that the aggregate root is the only way to change an aggregate, so the repository must not offer a back door. If there is no way to save an order line except by saving its order, the order's invariants, such as the total matching the lines, cannot be bypassed.
| DAO | DDD repository | |
|---|---|---|
| Shaped around | A table | An aggregate |
| How many | One per table, including child tables | One per aggregate root |
| Typical methods | insert, update, delete, findByColumn | findById, add, remove, domain queries |
| Returns | Rows or flat records | Whole aggregates |
| Where the interface lives | Persistence layer | Domain layer |
Notice that there is no OrderLineRepository. Lines are inside the Order aggregate, so they are loaded and saved with it.
3One Repository per Aggregate Root
Because repositories serve aggregates, the rule is one repository per aggregate root, not per table and not per entity. An Order aggregate with an orders table and an order_lines table has one OrderRepository. A Customer aggregate with three tables has one CustomerRepository.
This rule is also a useful design check. If we feel we need a repository for an inner entity, for example to load a single order line by id from somewhere else in the code, that is a sign either that the code should go through the order, or that the line is really its own aggregate.
4Where Spring Data Fits
Spring Data generates the implementation of a repository interface at runtime. We declare an interface that extends Repository, CrudRepository or JpaRepository, and Spring creates a proxy that implements save, findById, delete and any derived query methods such as findByStatus.
This works very well as a DDD repository with two conditions. First, we create Spring Data repositories only for aggregate roots, never for inner entities. Second, we think about what we expose. Extending JpaRepository brings dozens of methods, including flush, deleteAllInBatch and getReferenceById, which leak persistence concerns. Extending the bare Repository interface and declaring only the methods we want keeps the repository in domain terms. Spring Data even provides AbstractAggregateRoot and @DomainEvents for publishing events when a root is saved, which shows how directly it was designed around DDD.
5Interfaces in the Domain, Implementations in the Infrastructure
In a layered or hexagonal design, the repository interface belongs to the domain layer, next to the aggregate it serves, because it describes something the domain needs: a way to get and keep orders. The implementation belongs to the infrastructure layer, because it knows about JPA, SQL or a document store.
With Spring Data the implementation is generated, so the separation is mostly about dependencies: domain services depend on OrderRepository, and nothing in the domain imports JPA query classes or EntityManager. If we need a custom query that Spring Data cannot derive, we add a fragment interface with an implementation class in the infrastructure package. And if we later keep a separate persistence model, the domain interface can stay the same while an adapter maps between the two.
- Application servicesuse
OrderRepositoryto load and save orders - Domain layerOrder aggregate and the
OrderRepositoryinterface - Infrastructure layerSpring Data proxy, custom fragments, JPA mappings
- Databaseorders and order_lines tables
4 layers, from the top down. Step through them one at a time.
6Follow-up questions
Is it wrong to use JpaRepository directly?
It is not wrong, and many teams do it pragmatically. The risk is that its many generic methods invite persistence thinking into services, such as calling saveAndFlush everywhere or deleting by id without the root's checks. Narrowing the interface costs a few lines and keeps intent clear.
Where do reporting queries go?
Not in aggregate repositories. A report that joins many tables and returns flat rows is a read concern. A separate query service, a Spring Data projection interface or plain SQL through JdbcClient fits better and does not need to load aggregates.
Does save() need to be called after changing a loaded aggregate?
With JPA inside a transaction, no: the persistence context tracks loaded entities and flushes changes at commit. Calling save anyway is harmless and keeps the code honest if the implementation is ever not JPA, so many teams call it explicitly.
7Conclusion
In this answer, we saw that a DDD repository gives collection-like access to aggregates, with find by id, add, remove and domain-specific queries, and that there is one repository per aggregate root, not per table or child entity. A DAO is usually table-oriented and exposes persistence operations. Spring Data generates repository implementations from interfaces, and those serve as DDD repositories when we limit them to aggregate roots and expose only the methods we need. The interfaces belong to the domain, while the implementations belong to the infrastructure.
Open with
A DAO and a DDD repository both hide the database, but they are shaped around different things. A DAO is usually table-oriented: an OrderDao and an OrderLineDao, with insert, update, delete and find methods that mirror the tables.
Then, if they want more
It exposes persistence operations, so any caller can update an order line row directly without the order checking its rules. A DDD repository is shaped around aggregates. It behaves like a collection of orders: find by id, add, remove, plus a few queries in business terms like open orders older than a week.
It returns whole aggregates and takes whole aggregates back, and there is exactly one per aggregate root, so there is an OrderRepository but no OrderLineRepository; lines are saved through the order, which protects the invariants. Spring Data fits this well because it generates the implementation from an interface.
I just have to use it in the DDD way: create repositories only for aggregate roots, and preferably extend the plain Repository interface and declare only the methods I need, rather than JpaRepository with all its persistence methods.
In terms of layers, the repository interface belongs to the domain, next to the aggregate, because it expresses what the domain needs, and the implementation, generated by Spring Data plus any custom fragments, belongs to the infrastructure. Reporting queries I keep out of these repositories, in a separate query service.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does a repository in DDD differ from a DAO, and how does Spring Data fit into this?": https://scrivio-interview-library.netlify.app/java/domain-driven-design/67c6669e3b/