Grails 域设计:继承还是 class 具有可空属性?

Grails Domain Design: inheritance or one class with nullable properties?

我有一个域名OrderItem

class OrderItem {
    Product product
    Packaging packaging
    //other details
}

class Product {
 //other details
}

class Packaging {
//other details
}

目前,OrderItem 仅指 Beers。产品是啤酒的类型。包装是cans/bottles/keg/etc.

然后客户决定也出售不是啤酒的物品:烈酒、配料、纪念品等。

我认为这些都是 OrderItem 对象,但系统当前将 OrderItem 视为 Beers。我应该如何引入扩展模型? (注意:部分新品没有产品、包装或两者都没有。)

  1. 继承?仍将 OrderItem 仅视为 Beers。然后所有新项目将有自己的扩展 OrderItem 的 class。优点和缺点? Table 每个层级或每个 class?
  2. 给OrderItem添加属性(有些可以为空)来区分新的商品?比如'category'、'someItemCode'、'someItemPricing'等

我更喜欢第二个,添加额外的 属性,例如 categoryitemType

因此,在添加新字段时,您的迁移脚本必须将所有现有记录的 itemType 字段值设置为 Beers(因为您拥有的所有产品都是 Beers) .

我不知道您的哪些字段将成为 null,但是如果您的 OrderItem table 仍然有许多 nullable 列,您需要对其进行标准化它。