PoEAA ch12 Single Table Inheritance
Single Table Inheritance
Represents an inheritance hierarchy of classes as a single table
that has columns for all the fields of the various classes.
- RDBは「継承」をサポートしない
- DBとオブジェクトとのマッピングの際には、継承構造をRDB上で表現する方法を考える必要がある
- いろいろある
- Single Table Inheritance
- Class Table Inheritance
- Concrete Table Inheritance
- いろいろある
- Single Table Inheritanceは、テーブルのJOINを最小化する向きのもの
How It Works
コード | クラス名 | |
---|---|---|
クラス名への変換 | 必要 | 不要 |
空間効率 | 良い | 悪い |
テーブルを直接触りやすい | o | x |
- 【所感】 リレーショナルモデル原理主義者は怒りそう
- NULLが入ると第一正規形ですらない
- リレーション分割しろ
When to Use It
- 他のマッピングパターン
- Class Table Inheritance
- Concrete Table Inheritance
- 比較してのメリデメ
- メリ
- テーブル1つだけ考えれば良い
- JOINなし
- クラス側で、フィールドを親もしくは子に移動するリファクタリングがDBに跳ねない
- デメ
- メリ
- 複数のTable Inheritanceパターンを混用してもよい
- 似ているクラスはSingle Table Inheritanceでまとめる
- 特有のデータが多いクラスはConcrete Table Inheritanceで切り出す
コード例
- 略
- かいつまんでメモ
- Mapperつくる
- 下記の依存を断ち切る
- 永続化するDomain Modelクラス
- DBゲートウェイクラス
- 下記の依存を断ち切る
- MapperはDomain Modelの具象クラスごとに作る
- 型フィールドで判定して使い分ける
- Mapperつくる
英語
- mount up
- to gradually increase in number or amount
- to gradually become larger in amount