将继承的对象存储在数据库中 [英] Storing inherited objects in a database

查看:115
本文介绍了将继承的对象存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出将对象模型中的继承关系映射到关系数据库中的最佳方法。例如考虑以下类结构。

  public Class Item 
{
public String Name {get; set;}
public int Size {get; set}
}

public Class Widget:Item
{
public String Color {get; set;}
}

public Class Doohicky:Item
{
public String Smell {get; set;}
}

以下是我正在考虑如何保存的几个选项



选项1:所有项目类型的单表

 项目表:ItemID,Name,Color,Smell 



选项2:为每种项目类型分隔表

 窗口小部件表:WidgetID,Name,Color 
Doohicky表:DoohickyID,Name,Smell

这是更好的,但更难列出所有项目



选项3:链接表

 项目表:ItemID(PK),名称,大小
小部件表:WidgetID(PK) ItemID(FK),Color
Doohicky表:DoohickyID(PK),ItemID(FK),Smell

我认为这个选项是最好的,因为它阻止我在任何字段中使用Null值,加上它可以更容易列出所有的项目,和/或创建一个特定类型的项目列表(小部件或Doohickies)。



但是,我不知道如何创建Items表和Widgets和Doohickies表之间的关系。我不想在表中引用相同ItemID的表中最后一行。



例如,当我向Widgets表中添加一个条目时,如何确保将它与Items表中的新条目链接到唯一的ItemID?我应该只跟踪ItemID,而不是像WidgetID和DoohickyID那样分离类型特定的ID,并使用它来创建Items表和类型特定表之间的一对一的关系?



选项4

 项目表:ItemID(PK),名称,大小
小部件表:ItemID(PK),Color
Doohicky表:ItemID(PK),Smell


解决方案

您正在描述单表继承具体表继承类表继承



请参阅经典的Martin Fowler书籍企业应用程序架构有很多关于它们的信息。



您可以做一些事情来确保只有一个子类型可以引用超类型是使用 ItemType 字段。外键可以引用UNIQUE约束的列以及PRIMARY KEY。因此,您可以添加 Items.ItemType ,并对 ItemID ItemType 。每个子类型表中的外键引用这个两列唯一键。然后,您可以将每个子类型表中的 ItemType 约束为一定值。那么它必须匹配超类型表中的值,它只能有一个值。

 项目表:ItemID(PK ),ItemType,Name,Size 
UNIQUE:(ItemID,ItemType)
窗口小部件表:ItemID(PK),ItemType,Color
FK:(ItemID,ItemType)
CHECK: ItemType = W
Doohicky表:ItemID(PK),ItemType,Smell
FK:(ItemID,ItemType)
CHECK:ItemType = D
/ pre>

I'm trying to figure out the best way to map inheritance relationships in an object model into a relational database. For example consider the following class structure.

public Class Item 
{
    public String Name{get; set;}
    public int Size {get; set}    
}

public Class Widget:Item
{
    public String Color{get; set;}
}

public Class Doohicky:Item
{
    public String Smell{get; set;}
}

Here's are a few options I'm considering for how to save this structure to a database.

Options 1: Single Table for all item types

Items Table: ItemID, Name, Color, Smell

This sucks as it would require NULL values.

Options 2: Separate tables for each item type

Widgets Table: WidgetID, Name, Color
Doohicky Table: DoohickyID, Name, Smell

This is better, but would be more difficult to list all Items

Options 3: Linked tables

Items Table: ItemID (PK), Name, Size
Widgets Table: WidgetID (PK), ItemID (FK), Color
Doohicky Table: DoohickyID (PK), ItemID (FK), Smell

I think this option is the best, as it prevents me from having Null values in any fields, plus it will make it easier to list all the Items, and/or create a list of a specific type of Item (Widgets or Doohickies).

However, I'm not sure how to create the relationship between the Items table and the Widgets and Doohickies tables. I don't want to end up with row in either table referencing the same ItemID in the Items table.

For example when I add an entry to the Widgets table, how can I ensure that it is linked to a new entry in the Items table with a unique ItemID? Should I instead only track the ItemID rather then separate type specific IDs like WidgetID and DoohickyID, and use it to create a one to one relationships between the Items table and the type specific tables?

Options 4

Items Table: ItemID (PK), Name, Size
Widgets Table: ItemID (PK), Color
Doohicky Table: ItemID (PK), Smell

解决方案

You're describing Single-Table Inheritance, Concrete Table Inheritance, and Class Table Inheritance.

See the classic Martin Fowler book Patterns of Enterprise Application Architecture for lots of information about them.

One thing you can do to make sure only one sub-type can reference the super-type is to use an ItemType field. A foreign key can reference the columns of a UNIQUE constraint as well as a PRIMARY KEY. So you can add Items.ItemType and make a unique constraint over ItemID, ItemType. The foreign key in each sub-type table references this two-column unique key. And then you can constraint the ItemType in each sub-type table to be a certain value. Then it must match the value in the super-type table, which can only have one value.

Items Table: ItemID (PK), ItemType, Name, Size
  UNIQUE: (ItemID, ItemType)
Widgets Table: ItemID (PK), ItemType, Color
  FK: (ItemID, ItemType)
  CHECK: ItemType=W
Doohicky Table: ItemID (PK), ItemType, Smell
  FK: (ItemID, ItemType)
  CHECK: ItemType=D

这篇关于将继承的对象存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆