Quick revision
The short answer
An aggregate is a cluster of objects, such as an Order and its order lines, that we treat as one unit for consistency, and the aggregate root is the one entity, here the Order, through which all outside code must go. The root enforces the aggregate's rules, a transaction usually changes only one aggregate, and each root normally has its own repository. One aggregate refers to another only by id so that object graphs stay small, nothing loads a huge graph through lazy associations, and the consistency boundaries stay clear.
What to remember
In this answer, we saw that an aggregate is a cluster of objects treated as one unit for consistency, with one root entity as the only entry point. The root enforces the invariants, outside code never modifies inner objects directly, and a transaction usually changes only one aggregate. Referring to other aggregates by id keeps object graphs small, avoids loading large graphs through lazy associations, and keeps the boundaries clear, and each aggregate root usually has its own repository.
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.
An aggregate is a cluster of objects, such as an Order and its order lines, that we treat as one unit for consistency, and the aggregate root is the one entity, here the Order, through which all outside code must go. The root enforces the aggregate's rules, a transaction usually changes only one aggregate, and each root normally has its own repository. One aggregate refers to another only by id so that object graphs stay small, nothing loads a huge graph through lazy associations, and the consistency boundaries stay clear.
Why this question is asked
Aggregates are the part of domain-driven design that most directly shapes how we write JPA entities and transactions. Get them wrong and we see the familiar symptoms: entities that load half the database, rules that are checked in one service and forgotten in another, and transactions that lock far more than they need. Interviewers ask this to see whether we know the rules and, more importantly, the reasons behind them. In this answer, we will recap what an aggregate is, see how the root protects the rules, look at the one-aggregate-per-transaction guideline, understand why references between aggregates use ids, and see how repositories fit in.
1A Quick Recap: What Is an Aggregate?
In DDD, an entity is an object with an identity, and a value object is an object defined by its attributes. Real business objects are rarely single entities. An order has lines, each line has a product reference, a quantity and a price, and the order has rules that involve all of them, such as the total must equal the sum of the lines, or a shipped order cannot get new lines.
An aggregate is the group of entities and value objects that must stay consistent together, treated as one unit when we load, change and save it. One entity in the group is the aggregate root. It is the only object outside code may hold a reference to or call methods on. The other objects inside, here the order lines, are reached only through the root.
- Order (aggregate root)the only entry point; enforces rules
- OrderLine entitiesinside the boundary; changed only through Order
- Money, Quantity value objectsinside the boundary; immutable
- customerId, productIdreferences to other aggregates, by id only
4 layers, from the top down. Step through them one at a time.
2How the Root Enforces the Rules
The rules that must always hold for an aggregate are called invariants. The root is responsible for them, which means every change goes through a method on the root that can check them. Outside code never reaches into the inner objects to change them directly.
Notice that the list of lines is returned as an unmodifiable copy. If we returned the real list, any caller could add a line without updating the total or checking the status, and the invariant would silently break. Keeping all changes behind the root's methods is what makes the aggregate a real consistency boundary rather than a naming convention.
3One Aggregate per Transaction
A transaction should usually change only one aggregate. The reason is that the aggregate is our unit of consistency: everything inside it must be correct when the transaction commits, and everything outside it may be updated a little later. If one transaction changes an Order, a Customer and the Inventory together, it locks rows of three aggregates, conflicts with anyone working on any of them, and makes it impossible later to split those aggregates into different services or databases.
When a change in one aggregate must cause a change in another, such as placing an order reserving stock, we usually publish a domain event and let a separate transaction update the second aggregate. That is eventual consistency, and it is fine whenever the business can tolerate a short delay. If it truly cannot, the two objects probably belong in the same aggregate.
4Why Refer to Other Aggregates Only by Id
It is natural in JPA to write @ManyToOne Customer customer inside Order. The trouble is that this makes the customer, and everything the customer refers to, reachable from every order. One careless call, order.getCustomer().getAddresses(), and a lazy association quietly loads another part of the database, perhaps in a loop, perhaps outside a transaction where it throws LazyInitializationException.
With an id, the graph we load for an order is just the order and its lines. Nobody can change a customer through an order by accident, because the order has no customer object to change. And when we do need the customer, we load it through its own repository, which makes the extra query visible in the code. As a bonus, an id reference keeps working if the customer later moves to another service, while an object reference does not.
5Repositories for Aggregate Roots
Since outside code only works with roots, each aggregate root usually has its own repository and the inner entities have none. There is an OrderRepository, but no OrderLineRepository; to change a line, we load the order, call a method on it and save the order. With JPA, cascade settings such as cascade = CascadeType.ALL and orphanRemoval = true on the lines make saving the root save the whole aggregate. This keeps every change flowing through the root, so the invariants are always checked.
6Follow-up questions
How big should an aggregate be?
As small as the invariants allow. We include only the objects that must be consistent with each other in the same transaction. A useful test is to ask, for each part, whether the business would really be harmed if it were updated a second later than the rest. If not, it can live in its own aggregate.
Can an aggregate contain another aggregate's root?
No. If two roots are inside one boundary, one of them is not really a root. An aggregate can hold the other root's id, and the two are kept in step through events or application services.
What if a screen needs data from several aggregates?
Reads do not need to go through aggregates. A query service or a read model can join tables or combine data for display. Aggregates exist to protect rules during changes, not to shape every read.
7Conclusion
In this answer, we saw that an aggregate is a cluster of objects treated as one unit for consistency, with one root entity as the only entry point. The root enforces the invariants, outside code never modifies inner objects directly, and a transaction usually changes only one aggregate. Referring to other aggregates by id keeps object graphs small, avoids loading large graphs through lazy associations, and keeps the boundaries clear, and each aggregate root usually has its own repository.
Open with
An aggregate is a group of objects that must stay consistent together, so we treat them as one unit when we load, change and save them. For example an Order with its order lines.
Then, if they want more
One entity is the aggregate root, here the Order, and it is the only entry point: outside code holds references only to the root and changes things only through its methods. That is how the root enforces the invariants, like the total matching the lines or no new lines on a shipped order.
I return inner collections as read-only copies so nobody can bypass it. A transaction should normally change just one aggregate, because the aggregate is the unit of consistency; if placing an order also has to reserve stock, I publish an event and update the other aggregate in its own transaction.
Between aggregates I refer by id, so Order holds a CustomerId rather than a @ManyToOne Customer. That keeps the loaded graph small, avoids lazy loading pulling in half the database, prevents someone changing a customer through an order, and makes the boundary obvious; if I need the customer I load it explicitly from its own repository.
And that is the last point: each aggregate root usually gets its own repository, and inner entities like order lines get none, so every change goes through the root.
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 "What is an aggregate and an aggregate root in DDD? Why should one aggregate refer to...": https://scrivio-interview-library.netlify.app/java/domain-driven-design/6ef7ae3163/