两个不同聚合的同一实体
Same entity for two different aggregate
我的架构将类似于上图。
我打算使用 Spring 数据 JDBC 并发现
如果多个聚合引用同一个实体,则该实体不能成为引用它的那些聚合的一部分,因为它只能是一个聚合的一部分。
以下是我的问题:
- 如何在不更改数据库设计的情况下为上述内容创建两个不同的聚合?
- 如何单独检索订单/供应商列表?即我不想遍历聚合根。
How to create two different aggregates for the above without changing the DB design?
我认为您这里只有三个聚合:Order、Vendor 和 ProductType。我经常使用的心理测试是:
If A
has a reference to B
and I delete an A
, should I automatically and without exception delete all B
s referenced by that A
? If so B
is part of the A
Aggregate.
对于图表中的任何关系,这似乎都不是真的,所以让我们为每个实体使用单独的聚合。
这又使图表中的每个引用成为不同聚合之间的一个。
如 "Spring Data JDBC, References, and Aggregates" 中所述,这些必须在您的 Java 代码中建模为 ID,而不是 Java 引用。
class Order {
@Id
Long orderid;
String name;
String description;
Instance created;
Long productTypeId;
}
class Vendor {
@Id
Long vid;
String name;
String description;
Instance created;
Long productTypeId;
}
class ProductType {
@Id
Long pid;
String name;
String description;
Instance created;
}
因为它们是独立的聚合体,每个都有自己的存储库。
interface Orders extends CrudRepository<Order, Long>{
}
interface Vendors extends CrudRepository<Vendor, Long>{}
interface ProductTypes extends CrudRepository<ProductType, Long>{}
至此我认为我们满足了您的要求。您可能需要添加一些 @Column
和 @Table
注释以获得您想要的确切名称或提供 NamingStrategy
.
您可能还需要对产品类型进行某种缓存,因为我希望它们看到大量读取而写入很少。
当然,您可以向存储库添加其他方法,例如:
interface Orders extends CrudRepository<Order, Long>{
List<Orders> findByProductTypeId(Long productTypeId);
}
我的架构将类似于上图。
我打算使用 Spring 数据 JDBC 并发现
如果多个聚合引用同一个实体,则该实体不能成为引用它的那些聚合的一部分,因为它只能是一个聚合的一部分。
以下是我的问题:
- 如何在不更改数据库设计的情况下为上述内容创建两个不同的聚合?
- 如何单独检索订单/供应商列表?即我不想遍历聚合根。
How to create two different aggregates for the above without changing the DB design?
我认为您这里只有三个聚合:Order、Vendor 和 ProductType。我经常使用的心理测试是:
If
A
has a reference toB
and I delete anA
, should I automatically and without exception delete allB
s referenced by thatA
? If soB
is part of theA
Aggregate.
对于图表中的任何关系,这似乎都不是真的,所以让我们为每个实体使用单独的聚合。
这又使图表中的每个引用成为不同聚合之间的一个。
如 "Spring Data JDBC, References, and Aggregates" 中所述,这些必须在您的 Java 代码中建模为 ID,而不是 Java 引用。
class Order {
@Id
Long orderid;
String name;
String description;
Instance created;
Long productTypeId;
}
class Vendor {
@Id
Long vid;
String name;
String description;
Instance created;
Long productTypeId;
}
class ProductType {
@Id
Long pid;
String name;
String description;
Instance created;
}
因为它们是独立的聚合体,每个都有自己的存储库。
interface Orders extends CrudRepository<Order, Long>{
}
interface Vendors extends CrudRepository<Vendor, Long>{}
interface ProductTypes extends CrudRepository<ProductType, Long>{}
至此我认为我们满足了您的要求。您可能需要添加一些 @Column
和 @Table
注释以获得您想要的确切名称或提供 NamingStrategy
.
您可能还需要对产品类型进行某种缓存,因为我希望它们看到大量读取而写入很少。
当然,您可以向存储库添加其他方法,例如:
interface Orders extends CrudRepository<Order, Long>{
List<Orders> findByProductTypeId(Long productTypeId);
}