代码之家  ›  专栏  ›  技术社区  ›  Andrew Shepherd

ORM:针对两个引用实体指定值相等约束

  •  1
  • Andrew Shepherd  · 技术社区  · 15 年前

    我正在尝试使用对象角色建模来建模概念,但找不到必需的约束类型。我想知道它是否存在。

    以下是三个事实:

    • 商品必须属于同一商品类别
    • EntityDescriptor必须属于CommodityCategory
    • EntityDescriptor可以用于一种商品

    这很容易建模:

    alt text

    但这里有个限制:

    • 如果EntityDescriptor用于商品,则商品引用的CommodityCategory必须等于EntityDescriptor引用的CommodityCategory

    例如,假设我们有这些商品。

    *--------------------*------------*
    | CommodityCategory  | Commodity  |
    *--------------------*------------*
    | Fuel               | Gas        |
    | Fuel               | Petrol     |
    | Food               | Sugar      |
    *--------------------*------------*
    

    这些是合法的

    *------------------*-------------------*-----------*
    | EntityDescriptor | CommodityCategory | Commodity |
    *------------------*-------------------*-----------*
    | 1                | Fuel              |           |
    | 2                | Fuel              | Gas       |
    | 3                | Food              |           |
    | 4                | Food              | Sugar     |
    *------------------*-------------------*-----------*
    

    但这是违法的

    *------------------*-------------------*-----------*
    | EntityDescriptor | CommodityCategory | Commodity |
    *------------------*-------------------*-----------*
    | 5                | Food              | Petrol    |
    *------------------*-------------------*-----------*
    

    我看了看 平等 约束,但这是关系的存在,而不是关系中的实际价值。

    有什么东西可以用来建立这个约束的模型吗?

    3 回复  |  直到 15 年前
        1
  •  1
  •   cliffordheath    12 年前

    用CQL书写 see the ActiveFacts home page ,您需要这样的子集约束:

    some EntityDescriptor references some Commodity
        only if that EntityDescriptor is for some CommodityCategory and that Commodity is of that CommodityCategory;
    

    注意,如果在每个方向上都包含一个读数,这将变得更加流畅。

    在NORMA中,需要具有两个角色对的子集约束:

    子集对是“EntityDescriptor引用商品”的两个角色。 超集对是EntityDescriptor在“EntityDescriptor用于CommodityCategory, 商品在“商品是商品范畴”中的作用。

    注意,每对的第一个角色由同一类型(EntityDescriptor)扮演, 同样,每一对(商品)的第二个角色。也可以使用兼容的子类型/超类型,但这些类型必须以这种方式兼容。

    相等约束就像两个子集约束,每个方向上运行一个子集约束。它总是要求至少有一个引用存在,只要一个实体描述符是针对某个商品类别的,并且该商品是该商品类别的, 反之亦然 .

        2
  •  1
  •   ikhsan    15 年前

    我们不能在 属于 是为了 ,因此每个商品到类别都是实体描述符到类别的子集

    表如下:ED(EntityDescriptor)、CC(CommodityCategory)、CM(Commodity)

    ED CC   <--->  CC  CM  ED   <---> CM CC
    1  1           1   1   1          1  1 
    2  2           2   2   2          2  2
    3  3                              5  5 // error, cause CC doesn't have 5,5 to ED
    4  4           4   4   4          4  4 
    5  4           4   5   4          5  4 // ok, cause CC have 4 to 5 on CC-ED
    6  4                              6  3 // error, cause ED-CC doesn't have 6,3
    

    所以我们可以看到CC有两个角色( r1级 )和到厘米( r2型 ),那个 r2型 是的子集 r1级 . 所以我认为商品对ED没有直接的约束作用,而是通过CC应用的约束。

        3
  •  0
  •   orangepips 111111    15 年前

    如果要强制使用数据库,我建议在插入/更新触发器之前使用该数据库,以防止将EntityDescription与不匹配的商品关联起来。

    如果你想使用代码,我建议你学习 Specification Pattern . 假设一个Commodity、EntityDescriptor和CommodityDescriptor类。CommodityDescriptor将是其他两个类的组成部分。商品将包括一个规范,比如matchingcommitteydescriptionspecification(是的,它很冗长)作为其组成部分。然后,当调用Commodity.setEntityDescription(EntityDescription-entityDescriptor)时,它通过比较Commodity和entityDescriptor的CommodityDescriptor值来根据规范进行验证。

    推荐文章