概述:
关系数据库的表之间不存在继承关系,为了将面向对象中的继承关系映射到关系数据库中,可以使用以下三种继承映射策略:
每个继承层次一张表
每个具体类一张表
每个类一张表
每个继承层次一张表
这种映射方式整个继承层次结构中所有类的属性映射为一张表中的字段。
增加鉴别字段:为了区分来自不同的类的映射,需要在表中增加一个用于鉴别不同类实体的字段。
映射语法:
分别为继承层次的各个类定义持久化类,其中
只有父类包含标识符属性
定义一个以父类命名的映射文件
在映射文件中的后面添加
元素,并使用该元素的column属性定义鉴别
字段
在映射文件中,父类的属性直接放在标
记对之间,子类的属性放在标记对
之间
在类/子类标记中使用discriminator-value
属性指定各个类对应的鉴别字段的值
每个继承层次一张表策略的优缺点:
优点:最简单、执行效率最高(因为无需进行任何关联操作)。
缺点:存在冗余字段;在数据表中需要加入额外的区分各个类的字段;同时不允许为子类的属性对应的字段定义为not null约束
每个具体类一张表
这种映射方式为每个子类创建一张表,每张表包
含子类的所有属性,包括继承的父类的属性
映射语法:
分别为继承层次的各个类定义持久化类,其中只有父类包含标识符属性
定义父类为抽象类,每个子类为具体类
定义一个以父类命名的映射文件
父类不映射成表,每个子类映射一张表
在映射文件中,父类属性放在标记对之间设置,子类属性放在标记对之间设置
每个具体类一张表策略的优缺点:
优点:数据结构清晰,且可以对子类的属性
映射的字段定义not null约束
缺点:子表的主键不能重复,不能使用数据库的
自增方式生成主键;父类属性重复出现在多张表
中
每个类一张表
这种映射方式为每个类创建一张表,每张表仅包含当前类的属性,不包含父类的属性。
映射语法:
分别为继承层次的各个类定义持久化类,其中只有父类包含标识符属性
定义一个以父类命名的映射文件
父类和子类分别映射到不同的表,父类与子类对应的表通过外键关联
在映射文件中,父类属性放在标记对之间,子类属性放在映射文件中的标记对之间
每个子类一张表策略的优缺点
优点:数据结构层次清晰,没有冗余,且可以对子类的属性映射的字段定义not null约束
缺点:类的继承层次比较多时,需要关联的表也越多,查询性能不如每个类继承结构一张表
示例:
上传之后我会把链接放到这里
pom.xml导入的依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/antlr/antlr -->
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.10.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml/classmate -->
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.fastinfoset/FastInfoset -->
<dependency>
<groupId>com.sun.xml.fastinfoset</groupId>
<artifactId>FastInfoset</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.22.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.istack/istack-commons-runtime -->
<dependency>
<groupId>com.sun.istack</groupId>
<artifactId>istack-commons-runtime</artifactId>
<version>3.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss/jandex -->
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>2.1.3.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.24.0-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/javax.activation-api -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.logging/jboss-logging -->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.spec.javax.transaction/jboss-transaction-api_1.2_spec -->
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<version>1.1.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jvnet.staxex/stax-ex -->
<dependency>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
<version>1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/txw2 -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>txw2</artifactId>
<version>2.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.4.22.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/mchange-commons-java -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.4.22.Final</version>
</dependency>
</dependencies>