Card 04/ 07
All 7 cards
ConceptDifficulty: Advanced1 min
One Repository Per Aggregate
Spring Data JDBC doesn't let you write a repository for just any entity. It forces a rule most JPA applications never have to think about.
In Spring Data JDBC, an aggregate root is the one entity a repository is written for, and everything it owns — its child entities, its collections — is loaded and saved as part of it, as one unit. The rule that falls out of this: one repository per aggregate root. A child entity reachable only through its parent doesn't get a repository of its own; asking for one directly would mean two independent paths to data that's only supposed to be consistent as a whole.
JPA has no equivalent restriction — you can write a repository for any @Entity, parent or child, which is part of what makes a JPA domain model's true shape harder to see from its repositories alone.