Single Table Inheritance

Represents an inheritance hierarchy of classes as a single table that has columns for all the fields of the various classes.


These are the strengths of Single Table Inheritance:

  • There's only a single table to worry about on the database.

  • There are no joins in retrieving data.

  • Any refactoring that pushes fields up or down the hierarchy doesn't require you to change the database.

The weaknesses of Single Table Inheritance are

  • Fields are sometimes relevant and sometimes not, which can be confusing to people using the tables directly.

  • Columns used only by some subclasses lead to wasted space in the database. How much this is actually a problem depends on the specific data characteristics and how well the database compresses empty columns. Oracle, for example, is very efficient
    in trimming wasted space, particularly if you keep your optional columns to the right side of the database table. Each database has its own tricks for this.

  • The single table may end up being too large, with many indexes and frequent locking, which may hurt performance. You can avoid this by having separate index tables that either list keys of rows that have a certain property or that copy a
    subset of fields relevant to an index.

  • You only have a single namespace for fields, so you have to be sure that you don't use the same name for different fields. Compound names with the name of the class as a prefix or suffix help here.

Class Table Inheritance

Represents an inheritance hierarchy of classes with one table for each class.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamF2YV8xMTEx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

The strengths of Class Table Inheritance are

  • All columns are relevant for every row so tables are easier to understand and don't waste space.

  • The relationship between the domain model and the database is very straightforward.

The weaknesses of Class Table Inheritance are

  • You need to touch multiple tables to load an object, which means a join or multiple queries and sewing in memory.

  • Any refactoring of fields up or down the hierarchy causes database changes.

  • The supertype tables may become a bottleneck because they have to be accessed frequently.

  • The high normalization may make it hard to understand for ad hoc queries.

Concrete Table Inheritance

Represents an inheritance hierarchy of classes with one table per concrete class in the hierarchy.

The strengths of Concrete Table Inheritance are:

  • Each table is self-contained and has no irrelevant fields. As a result it makes good sense when used by other applications that aren't using the objects.

  • There are no joins to do when reading the data from the concrete mappers.

  • Each table is accessed only when that class is accessed, which can spread the access load.

The weaknesses of Concrete Table Inheritance are:

  • Primary keys can be difficult to handle.

  • You can't enforce database relationships to abstract classes.

  • If the fields on the domain classes are pushed up or down the hierarchy, you have to alter the table definitions. You don't have to do as much alteration as with
    Class Table Inheritance (285), but you can't ignore this as you can with
    Single Table Inheritance (278).

  • If a superclass field changes, you need to change each table that has this field because the superclass fields are duplicated across the tables.

  • A find on the superclass forces you to check all the tables, which leads to multiple database accesses (or a weird join).

Object-Relational Structural Patterns的更多相关文章

  1. Object Relational Tutorial 对象关系教程

    The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes ...

  2. Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM)

    Django中ORM介绍和字段及字段参数   Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简 ...

  3. Object Relational Mapping(ORM)

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  4. Object Relational Mapping框架之Hibernate

    hibernate框架简介: hibernate框架就是开发中在持久层中应用居多的ORM框架,它对JDBC做了轻量级的封装. (百度介绍,感觉不错) 什么是ORM:Object Relational ...

  5. ORM(Object Relational Mapping)框架

    ORM(Object Relational Mapping)框架 ORM(Object Relational Mapping)框架采用元数据来描述对象一关系映射细节,元数据一般采用XML格式,并且存放 ...

  6. 一:ORM关系对象映射(Object Relational Mapping,简称ORM)

    狼来的日子里! 奋发博取 10)django-ORM(创建,字段类型,字段参数) 一:ORM关系对象映射(Object Relational Mapping,简称ORM) ORM分两种: DB fir ...

  7. Object/Relational Mapping 数学关系 反面向对象

    [hibernate ORM 是对象关系映射框架 事实上的持久化存储引擎] http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/ ...

  8. Django中的Object Relational Mapping(ORM)

    ORM 介绍 ORM 概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用 ...

  9. 设计模式之美:Structural Patterns(结构型模式)

    结构型模式涉及到如何组合类和对象以获得更大的结构. 结构型类模式采用继承机制来组合接口实现. 结构型对象模式不是对接口和实现进行组合,而是描述了如何对一些对象进行组合,从而实现新功能的一些方法. 因为 ...

随机推荐

  1. Spring AOP分析(2) -- JdkDynamicAopProxy实现AOP

    上文介绍了代理类是由默认AOP代理工厂DefaultAopProxyFactory中createAopProxy方法产生的.如果代理对象是接口类型,则生成JdkDynamicAopProxy代理:否则 ...

  2. Java Enum用法详解

    Java Enum用法详解 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举 ...

  3. IE6中 PNG 背景透明的最佳解决方案

    为什么要使用 PNG 图片? 简 单来说,使用 PNG 格式比起 GIF 来表现色彩更丰富,特别是表现渐变以及背景透明的渐变要比GIF格式出色很多.目前,最新的浏览器基本上都支持PNG格式.唯独有万恶 ...

  4. padding-使用必记

    前言 说起了padding可谓是盒子模型中最常用的一个属性,你真的了解padding吗?那我请问您设置padding会影响盒子的宽度与高度吗?也许好多人会回答padding会影响到盒子的宽度与高度.在 ...

  5. 函数chdir、fchdir和getcwd

    函数chdir.fchdir和getcwd chdir.fchdir函数     每个进程都有一个当前工作目录,当前目录是进程的一个属性     当用户登录UNIX系统时,其当前工作目录通常是口令文件 ...

  6. 【Java入门提高篇】Day1 抽象类

    基础部分内容差不多讲解完了,今天开始进入Java提高篇部分,这部分内容会比之前的内容复杂很多,希望大家做好心理准备,看不懂的部分可以多看两遍,仍不理解的部分那一定是我讲的不够生动,记得留言提醒我. 好 ...

  7. JavaWeb面试(七)

    61,JDBC访问数据库的基本步骤是什么?1,加载驱动2,通过DriverManager对象获取连接对象Connection3,通过连接对象获取会话4,通过会话进行数据的增删改查,封装对象5,关闭资源 ...

  8. unique & lower_bound C++

    原来C++也有unique和lower_bound,只需头文件iostream unique unique可以对数组进行相邻元素的"去重",实现效果是把所有不重复的元素按顺序放在数 ...

  9. python基础教程——dict和set

    dict python内置字典:dict,全称dictionary,在其他语言中称为map,使用键值对存储. ex: d = {'xiaoli' : 95 , 'xiaoming' : 98 , 'x ...

  10. HTML5能否会成为Web技术的核心?

    谁会成为HTML5后继者? 那么会有一个HTML6吗? Jaffe表示,网上支付可能会推动进行这样的全面修订,以期能为网上支付来提供一个统一方式.如果大家将之称为HTML 6,那么HTML 6是极有可 ...