我想将我们的问题带给更多的听众,因为我们已经在公司中讨论了一段时间,但找不到答案。
让我们假设我们有一个交易对象,它是聚合根。在交易内部,我们有作为价值对象的金钱。
class Transaction {
private Money money;
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
和
class Money {
private BigDecimal amount;
private String currency;
}
Run Code Online (Sandbox Code Playgroud)
可以将这样的Transaction持久化(我们使用休眠方式)以将db转换为简单的表事务
+-----------------+
| transaction |
+-----------------+
|id : bigint |
|amount: decimal |
|currency: varchar|
|other... |
+-----------------+
Run Code Online (Sandbox Code Playgroud)
一切都会很好,但是...客户要求我们在数据库中有货币表(我们称其为字典表),并且每个有钱的表(包括交易)都需要指向货币表:
+-----------------+ +-----------------+
| transaction | |curency |
+-----------------+ +-----------------+
|id : bigint | +---> | id: bigint |
|amount: decimal | | | code: varchar |
|curr_id: bigint | …Run Code Online (Sandbox Code Playgroud)