jpa实体继承

继承映射

如果只想要生成一张表,可以指定映射策略

1
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

简单用法

父:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@Entity
@Data
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class People implements Serializable {

    private static final long serialVersionUID = 6254092813095169295L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    protected String name;
}

子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.scan.test.People;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class Student extends People {
    private static final long serialVersionUID = 2241366750854518413L;

    private int className;
}

获得一张表

CREATE TABLE people (
dtype varchar(31) NOT NULL,
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
class_name int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注意:无论多少子类都只生成一张表,通过dtype字段区分不同子类。应用场景为同一种类多个对象的数据储存

实验在父对象添加属性,在数据库能否生成成功

复杂用法

父:

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
27
28
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@Entity
@Data
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(indexes = {@Index(columnList = "uid", unique = true),
        @Index(columnList = "studentUid")})
public class People implements Serializable {

    private static final long serialVersionUID = 6254092813095169295L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    protected Integer uid;

    protected Integer studentUid;

    protected String name;
}

子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.scan.test.People;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class Student extends People {
    private static final long serialVersionUID = 2241366750854518413L;

    @Column(unique = true)
    private int className;
}

获得表:

CREATE TABLE people (
dtype varchar(31) NOT NULL,
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
student_uid int(11) DEFAULT NULL,
uid int(11) DEFAULT NULL,
class_name int(11) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY UK98584pow8829ecp9w7fwbvocw (uid),
UNIQUE KEY UK_n00mlaw19y2hphh71hwt4lc3l (class_name),
KEY IDXjt0gho5f24xg5n8c710r8723a (student_uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

不管是父类和子类添加的属性都在数据表映射成功

还可以指定其他两种映射策略,这边就不展开讲

部分继承

1
@MappedSuperclass

只有子类会生成数据表,父类的属性同样会写进子类的表中

简单用法

父:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@Data
@MappedSuperclass
public class People implements Serializable {

    private static final long serialVersionUID = 6254092813095169295L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    protected String name;
}

子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.scan.test.People;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class Student extends People {
    private static final long serialVersionUID = 2241366750854518413L;

    private int className;
}

创建数据表:

CREATE TABLE student (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
class_name int(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注意:是使用子类生成的数据表,子类的数据表会包含父类的属性对象,并且继承的父类的id定义

复杂用法

父:

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
27
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@MappedSuperclass
@Data
@Table(indexes = {@Index(columnList = "uid", unique = true),
        @Index(columnList = "studentUid")})
public class People implements Serializable {

    private static final long serialVersionUID = 6254092813095169295L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    protected Integer uid;

    protected Integer studentUid;

    protected String name;
}

子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.scan.test.People;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Entity;

/**
 * @author: thy
 * @date: 2019/9/4
 * @description:
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class Student extends People {
    private static final long serialVersionUID = 2241366750854518413L;

    private int className;
}

创建数据表:

CREATE TABLE student (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
student_uid int(11) DEFAULT NULL,
uid int(11) DEFAULT NULL,
class_name int(11) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注意:@table定义的索引都没有成功。在使用@MappedSuperclass的时候需要注意,因为父类不是entity对于table的定义在父类都不会生效。只有是针对属性的定义能够在子类生成的数据表里面映射

https://blog.csdn.net/heardy/article/details/7924192

https://www.ibm.com/developerworks/cn/java/j-lo-hibernatejpa/index.html