# 嵌入式实体

通过使用embedded columns,可以减少应用程序中的重复(使用组合而不是继承)。

嵌入列是一个列,它接受具有自己列的类,并将这些列合并到当前实体的数据库表中。

例如:

假设我们有UserEmployeeStudent实体。

这些属性都有少量的共同点,first namelast name属性。

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column()
    firstName: string;
    
    @Column()
    lastName: string;
    
    @Column()
    isActive: boolean;
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class Employee {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column()
    firstName: string;
    
    @Column()
    lastName: string;
    
    @Column()
    salary: string;
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class Student {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column()
    firstName: string;
    
    @Column()
    lastName: string;
    
    @Column()
    faculty: string;
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

我们可以做的是通过创建一个包含firstNamelastName的新类:

import {Entity, Column} from "typeorm";

export class Name {
    
    @Column()
    first: string;
    
    @Column()
    last: string;
    
}
1
2
3
4
5
6
7
8
9
10
11

然后"connect"实体中的这些列:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Name} from "./Name";

@Entity()
export class User {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column(type => Name)
    name: Name;
    
    @Column()
    isActive: boolean;
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Name} from "./Name";

@Entity()
export class Employee {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column(type => Name)
    name: Name;
    
    @Column()
    salary: number;
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Name} from "./Name";

@Entity()
export class Student {
    
    @PrimaryGeneratedColumn()
    id: string;
    
    @Column(type => Name)
    name: Name;
    
    @Column()
    faculty: string;
    
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Name实体中定义的所有列将合并为useremployeestudent

+-------------+--------------+----------------------------+
|                          user                           |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| nameFirst   | varchar(255) |                            |
| nameLast    | varchar(255) |                            |
| isActive    | boolean      |                            |
+-------------+--------------+----------------------------+

+-------------+--------------+----------------------------+
|                        employee                         |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| nameFirst   | varchar(255) |                            |
| nameLast    | varchar(255) |                            |
| salary      | int(11)      |                            |
+-------------+--------------+----------------------------+

+-------------+--------------+----------------------------+
|                         student                         |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| nameFirst   | varchar(255) |                            |
| nameLast    | varchar(255) |                            |
| faculty     | varchar(255) |                            |
+-------------+--------------+----------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

这种方式可以减少实体类中的代码重复。 你可以根据需要在嵌入式类中使用尽可能多的列(或关系)。 甚至可以在嵌入式类中嵌套嵌套列。