Domain Driven Design and Relational Databases

所有 Spring Data 模块都受到了领域驱动设计中“存储库”、“聚合”和“聚合根”概念的启发。这些概念对于 Spring Data JDBC 而言可能更为重要,因为在一定程度上,它们与使用关系数据库时的正常操作相反。

All Spring Data modules are inspired by the concepts of “repository”, “aggregate”, and “aggregate root” from Domain Driven Design. These are possibly even more important for Spring Data JDBC, because they are, to some extent, contrary to normal practice when working with relational databases.

聚合是一组实体,保证在对其进行原子更改时保持一致。一个经典的例子是带有 OrderItemsOrderOrder 上的属性(例如,numberOfItemsOrderItems 的实际数量一致)在进行更改时保持一致。

An aggregate is a group of entities that is guaranteed to be consistent between atomic changes to it. A classic example is an Order with OrderItems. A property on Order (for example, numberOfItems is consistent with the actual number of OrderItems) remains consistent as changes are made.

跨聚合的引用并不总是保证一致。它们最终会保持一致。

References across aggregates are not guaranteed to be consistent at all times. They are guaranteed to become consistent eventually.

每个聚合都恰好有一个聚合根,它是聚合中的一个实体。聚合只能通过该聚合根上的方法进行操作。这些是前面提到的原子更改。

Each aggregate has exactly one aggregate root, which is one of the entities of the aggregate. The aggregate gets manipulated only through methods on that aggregate root. These are the atomic changes mentioned earlier.

存储库是对持久性存储的一种抽象,它看起来像某种类型的所有聚合的集合。对于 Spring Data,总的来说,这意味着你希望每个聚合根都有一个 Repository。此外,对于 Spring Data JDBC,这意味着从聚合根可到达的所有实体都被视为该聚合根的一部分。Spring Data JDBC 假设只有聚合才具有指向存储聚合非根实体的表的外部键,并且没有其他实体指向非根实体。

A repository is an abstraction over a persistent store that looks like a collection of all the aggregates of a certain type. For Spring Data in general, this means you want to have one Repository per aggregate root. In addition, for Spring Data JDBC this means that all entities reachable from an aggregate root are considered to be part of that aggregate root. Spring Data JDBC assumes that only the aggregate has a foreign key to a table storing non-root entities of the aggregate and no other entity points toward non-root entities.

在当前实现中,由聚合根引用的实体由 Spring Data JDBC 删除并重新创建。

In the current implementation, entities referenced from an aggregate root are deleted and recreated by Spring Data JDBC.

你可以使用与你的工作方式和数据库设计相匹配的实现来覆盖存储库方法。

You can overwrite the repository methods with implementations that match your style of working and designing your database.