4

我需要一张表来存储金融交易的状态。这个事务的状态可以用这个类来粗略的建模。

class FinancialTransaction
{
    Integer txId,
    Money oldLimit,
    Money newLimit,
    Money oldBalance,
    Money newBalance,
    Date txDate
}
class Money
{   
    Currency curr,
    BigDecimal amount
}

我最初的架构设计如下所示:

CREATE TABLE tx
(
    txId bigint(20) unsigned NOT NULL,
    oldlimit_currency varchar(3) NULL,
    oldlimit_amount decimal(7,5) default 0.00,
    newlimit_currency varchar(3) NULL,
    newlimit_amount decimal(7,5) default 0.00,
    ----snipped----
    PRIMARY KEY (txId)
)

我担心两件事:

  1. 每笔交易都基于一种货币进行。关于是否需要支持可能以多种货币发生的交易,我还没有想得足够多。假设它没有改变;那么只维护一个货币栏不是更节省空间吗?我会后悔这个简单的解决方案吗?
  2. 由于每个 Money 项目都是一个值对象,我是否应该将所有 Money 对象保存到单独的 Money 表中,并让原始表使用 moneyIds 作为 Money 表的外键?

那是,

CREATE TABLE tx
(
    txId bigint(20) unsigned NOT NULL,
    oldlimit_money_id int NOT NULL,
    newlimit_money_id int NOT NULL,
    ----snipped----
    PRIMARY KEY (txId),
    FOREIGN KEY (oldlimit_money_id) REFERENCES MONEY(id) ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY (newlimit_money_id) REFERENCES MONEY(id) ON DELETE NO ACTION ON UPDATE NO ACTION
)

有替代设计吗?

谢谢懒人网。

4

4 回答 4

4

货币和货币价值是两个不同的概念,因此最好将它们分开。没有必要为“价值”制作单独的表格,但最好为货币制作一个表格,因为它们是单独的实体。新设计看起来像:

CREATE TABLE tx
(
    id bigint(20) unsigned primary key,
    old_limit_currency_id int not null references CURRENCY(id),
    old_limit_value decimal(7,5) not null,
    new_limit_currency_id int not null references CURRENCY(id),
    new_limit_value decimal(7,5) not null
)

另外,检查小数(7,5)是否有足够的空间用于您的场景,它看起来有点低。有句老话:“比后悔更安全”:)

于 2009-01-09T08:18:00.293 回答
2
  1. 如果将来您确实需要支持两种货币之间的交易,则应该可以将其建模为两种交易,每种货币中的一种。
  2. 看起来您的 Money 对象在语义上是值而不是实体。因此,我认为没有必要将它们作为实体分开。
于 2009-01-09T08:08:26.897 回答
2

第三个想法呢:

CREATE TABLE tx
(
  txId bigint(20) unsigned NOT NULL,
  currency varchar(3) NOT NULL,
  oldlimit decimal(7,5) default 0.00,
  newlimit decimal(7,5) default 0.00,
  ----snipped----
  PRIMARY KEY (txId)
)

一笔交易的所有货币价值都必须是相同的货币,对吗?

于 2009-01-09T09:08:45.447 回答
-3

如果这不是矫枉过正,请更进一步,以单一货币存储所有金额/价值并维护汇率表。

于 2009-01-09T08:34:04.723 回答