Quick revision
The short answer
I would push to stop sharing one object graph across team boundaries. Rich bidirectional associations and cascades that cross modules couple teams through shared entities, so each module should define its own aggregates, reference other modules' aggregates by id instead of mapped associations, own its tables, and let other modules read through APIs, events or read models. We break the graph gradually, starting where cross-team changes are most frequent, and measure progress by how often a change still needs coordination across teams.
What to remember
In this answer, we saw that deep bidirectional mappings across modules couple teams through shared entities, cascades and joins. The long-term fix is aggregates per module, references to other modules by id, each module owning its tables, and other modules reading through APIs, events or read models. We break the graph gradually, starting with the seams that cause the most cross-team changes, protect each cut with architecture tests, and measure progress by how often a change still needs coordination across teams.
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.
I would push to stop sharing one object graph across team boundaries. Rich bidirectional associations and cascades that cross modules couple teams through shared entities, so each module should define its own aggregates, reference other modules' aggregates by id instead of mapped associations, own its tables, and let other modules read through APIs, events or read models. We break the graph gradually, starting where cross-team changes are most frequent, and measure progress by how often a change still needs coordination across teams.
Why this question is asked
A shared domain model often starts as a strength: one set of entities, every relationship navigable, everything consistent. Fifteen years and a dozen teams later, the same model is why a change to the Customer entity needs sign-off from six teams and a coordinated release. Interviewers ask this of the most senior candidates because the fix is structural and organisational at once, and doing it wrong, for example with a big-bang rewrite, can be worse than the problem. In this answer, we will recap how mapped associations couple teams, set the target structure, show what referencing by id looks like in code, plan the gradual change, and decide how to measure it.
1A Quick Recap: How Mapped Associations Couple Teams
In JPA, an association such as Order.customer or Customer.orders is more than a foreign key. It means the Order class imports the Customer class, Hibernate can load one from the other, cascades can create, update or delete across them, and queries join them freely. When Order belongs to the ordering team and Customer to the customer team, each of those mechanics becomes a dependency between teams.
So a rename in Customer breaks ordering code at compile time. A new eager association added by one team slows another team's queries. A cascade added for convenience lets one module delete another module's data. And because everything is in one persistence unit, a schema change needs everyone's migrations to run together. The cost the question describes, changes rippling across teams, is the visible sign of this coupling.
2The Target Structure
The goal is that each module owns a clear slice of the model and its data. Inside a module, rich mappings are fine: an Order with its lines, cascades and bidirectional helpers is one aggregate, a cluster of entities kept consistent together. Between modules, we use ids, not mapped associations. The ordering module stores customerId as a plain value, not a @ManyToOne Customer. Each module owns its tables, and no other module writes them or maps entities to them. When ordering needs customer data, it asks through the customer module's API, consumes its events, or keeps its own small read model of the fields it needs.
- Ordering moduleOrder aggregate, its own tables, stores
customerId - Module API and events
CustomerSummarylookups,CustomerChangedevents - Customer moduleCustomer aggregate, its own tables
- Shared platformdatabase server, migrations per module, no shared entities
4 layers, from the top down. Step through them one at a time.
Rich mappings stay inside a module; between modules only ids and contracts cross.
3Referencing by Id in Code
Here is the change in miniature. Before, the ordering module mapped the customer directly and even navigated into another team's associations.
Notice that ordering no longer knows how the customer team stores addresses. The customer team can split, rename or re-key its entities freely as long as CustomerSummary keeps its meaning. The database can keep the foreign key constraint between the tables while they live in the same database, which preserves integrity; what we remove is the Java and Hibernate coupling, not necessarily the constraint.
There is a cost: some queries that used to be a single join become two calls or need a read model. That is deliberate. It makes cross-team dependencies visible and intentional instead of hidden in a mapping.
4Breaking the Graph Gradually
A rewrite of hundreds of entities would stall every team for a year. Instead we pick the seams that hurt most. We mine the version history and incident reviews for the associations involved in cross-team changes, for example the last 50 changes that needed more than one team, and rank the entity pairs by frequency. The top few seams get cut first.
Each cut follows the same steps. Add the id field beside the association and populate it. Move readers in the other module to the API or read model. Remove cascades across the boundary. Remove the mapped association. Add an architecture test (ArchUnit or Spring Modulith's verification, for example) that fails the build if the ordering module imports customer entities again. Each step is small, can be released on its own, and leaves the system working.
- Rank seams by cross-team changesfrom history and incidents
- Add id beside the associationpopulate, keep both for now
- Move readers to API or read modelone call site at a time
- Remove cascades across the boundaryeach module deletes its own data
- Remove the associationid only
- Add an architecture testthe import cannot come back
6 steps in order. Play them, or step through them one at a time.
Small, releasable steps; the most painful seams first.
5Measuring Progress and Leading the Change
The measure that matters is how often a change requires coordination across teams. We can track it from pull requests that touch more than one module, releases that must be coordinated, and incidents caused by another team's change. If that number falls quarter by quarter, the structure is improving, whatever the entity count says.
The leadership work is as important as the technical plan. Teams must agree on module boundaries and ownership, which is often a conversation about who owns customer data, not about annotations. I would write the principles in a short decision record, make a platform team responsible for the shared tooling (API clients, event schemas, architecture tests), fund the first seams explicitly rather than hoping teams find time, and celebrate the first cut publicly so others see the benefit.
6Follow-up questions
Doesn't removing associations lose referential integrity?
Not necessarily. While the modules share a database, the foreign key constraint can stay. We remove the Java mapping and cascades, not the database guarantee. If modules later move to separate databases, integrity becomes eventual and is handled with events and reconciliation.
Is this the same as splitting into microservices?
No. Clear module boundaries inside one deployable, a modular monolith, give most of the benefit with less operational cost. Microservices may come later for a module that needs independent scaling, and the id-based boundaries make that much easier.
How do you handle reports that join across modules?
Through a read model or reporting database fed by events or change data capture, owned by whoever owns the report. Reporting needs are real, but they should not force every module to share its entities.
7Conclusion
In this answer, we saw that deep bidirectional mappings across modules couple teams through shared entities, cascades and joins. The long-term fix is aggregates per module, references to other modules by id, each module owning its tables, and other modules reading through APIs, events or read models. We break the graph gradually, starting with the seams that cause the most cross-team changes, protect each cut with architecture tests, and measure progress by how often a change still needs coordination across teams.
Open with
The root problem is that a shared object graph turns every association into a dependency between teams: imports, cascades, lazy loading and joins all cross the boundary, so a change in one team's entity ripples to everyone.
Then, if they want more
The structure I'd push for is aggregates per module. Inside a module, rich mappings are fine, like an Order with its lines. Between modules, we reference other aggregates by id instead of mapped associations, so Order stores a customerId rather than a ManyToOne Customer.
Each module owns its tables, nobody else writes them, and other modules read through an API, events, or a small read model of the fields they need. The database foreign key can stay while we share a database; what we cut is the Java and Hibernate coupling. I wouldn't do it as a rewrite.
I'd mine the history for changes that needed several teams, rank the entity pairs involved, and cut the worst seams first: add the id, move readers to the API, remove cascades, remove the association, and add an architecture test so it can't come back. Each step is small and releasable.
I'd measure success by how often a change still needs cross-team coordination, and on the leadership side, agree ownership of the data up front and fund the first cuts explicitly.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Your company's core domain model maps hundreds of JPA entities with deep bidirectional...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/0c11af8ca7/