# 实体继承

> 英文标题：Entity Inheritance
> 官网原文：https://typeorm.io/docs/entity/entity-inheritance
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 23 块

---

<!-- EN -->
# Entity Inheritance

<!-- ZH -->
# 实体继承

<!-- EN -->
## Concrete Table Inheritance

<!-- ZH -->
## 具体表继承

<!-- EN -->
You can reduce duplication in your code by using entity inheritance patterns. The simplest and the most effective is concrete table inheritance.

<!-- ZH -->
你可以通过使用实体继承模式来减少代码重复。其中最简单也最有效的是具体表继承（concrete table inheritance）。

<!-- EN -->
For example, you have `Photo`, `Question`, `Post` entities:

<!-- ZH -->
例如，你有 `Photo`、`Question`、`Post` 实体：

<!-- EN -->
```typescript
@Entity()
export class Photo {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    size: string
}
```

<!-- ZH -->
```typescript
@Entity()
export class Photo {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    size: string
}
```

<!-- EN -->
```typescript
@Entity()
export class Question {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    answersCount: number
}
```

<!-- ZH -->
```typescript
@Entity()
export class Question {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    answersCount: number
}
```

<!-- EN -->
```typescript
@Entity()
export class Post {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    viewCount: number
}
```

<!-- ZH -->
```typescript
@Entity()
export class Post {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string

    @Column()
    viewCount: number
}
```

<!-- EN -->
As you can see all those entities have common columns: `id`, `title`, `description`. To reduce duplication and produce a better abstraction we can create a base class called `Content` for them:

<!-- ZH -->
如你所见，这些实体都有公共列：`id`、`title`、`description`。为了减少重复并得到更好的抽象，我们可以为它们创建一个名为 `Content` 的基类：

<!-- EN -->
```typescript
export abstract class Content {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string
}
```

<!-- ZH -->
```typescript
export abstract class Content {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string
}
```

<!-- EN -->
```typescript
@Entity()
export class Photo extends Content {
    @Column()
    size: string
}
```

<!-- ZH -->
```typescript
@Entity()
export class Photo extends Content {
    @Column()
    size: string
}
```

<!-- EN -->
```typescript
@Entity()
export class Question extends Content {
    @Column()
    answersCount: number
}
```

<!-- ZH -->
```typescript
@Entity()
export class Question extends Content {
    @Column()
    answersCount: number
}
```

<!-- EN -->
```typescript
@Entity()
export class Post extends Content {
    @Column()
    viewCount: number
}
```

<!-- ZH -->
```typescript
@Entity()
export class Post extends Content {
    @Column()
    viewCount: number
}
```

<!-- EN -->
All columns (relations, embeds, etc.) from parent entities (parent can extend other entity as well) will be inherited and created in final entities.

<!-- ZH -->
父实体（父类也可以继续继承其他实体）的所有列（关系、嵌入等）都会被继承，并在最终实体中创建。

<!-- EN -->
This example will create 3 tables - `photo`, `question` and `post`.

<!-- ZH -->
这个示例会创建 3 张表——`photo`、`question` 和 `post`。

<!-- EN -->
## Single Table Inheritance

<!-- ZH -->
## 单表继承

<!-- EN -->
TypeORM also supports single table inheritance. Single table inheritance is a pattern when you have multiple classes with their own properties, but in the database they are stored in the same table.

<!-- ZH -->
TypeORM 也支持单表继承。单表继承是这样一种模式：你有多个各自带有属性的类，但在数据库中它们被存储在同一张表里。

<!-- EN -->
```typescript
@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class Content {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string
}
```

<!-- ZH -->
```typescript
@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export class Content {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    title: string

    @Column()
    description: string
}
```

<!-- EN -->
```typescript
@ChildEntity()
export class Photo extends Content {
    @Column()
    size: string
}
```

<!-- ZH -->
```typescript
@ChildEntity()
export class Photo extends Content {
    @Column()
    size: string
}
```

<!-- EN -->
```typescript
@ChildEntity()
export class Question extends Content {
    @Column()
    answersCount: number
}
```

<!-- ZH -->
```typescript
@ChildEntity()
export class Question extends Content {
    @Column()
    answersCount: number
}
```

<!-- EN -->
```typescript
@ChildEntity()
export class Post extends Content {
    @Column()
    viewCount: number
}
```

<!-- ZH -->
```typescript
@ChildEntity()
export class Post extends Content {
    @Column()
    viewCount: number
}
```

<!-- EN -->
This will create a single table called `content` and all instances of photos, questions and posts will be saved into this table.

<!-- ZH -->
这会创建一张名为 `content` 的表，所有照片、问题和帖子的实例都会被保存进这张表。

<!-- EN -->
## Using embeddeds

<!-- ZH -->
## 使用嵌入

<!-- EN -->
There is an amazing way to reduce duplication in your app (using composition over inheritance) by using `embedded columns`. Read more about embedded entities [here](https://typeorm.io/docs/entity/embedded-entities).

<!-- ZH -->
有一种很棒的方式可以减少应用中的重复代码（用组合优于继承），那就是使用 `embedded columns`（嵌入列）。在[这里](https://typeorm.io/docs/entity/embedded-entities)阅读更多关于嵌入式实体的内容。
