- 一.使用注解,继承映射写法
继承映射在Annotation中使用@Inheritance注解,并且需要使用strategy属性指定继承策略,继承策略有SINGLE_TABLE,TABLE_PER_CLASS和JOINED三种。
在这三种方法中查询速度:第二种方案 > 第一种方案 > 第三种方案。解耦程度:第三种方案 > 第一种方案 > 第二种方案
SINGLE_TABLE:将父类和及其所有子类集合在一块,存在一张表中,并创建一个新的字段来判断对象类型。
person.java
package com.xiaoxiao.model;
import javax.persistence.*;
/**
*
* @ClassName: Person
* @Description: 人类
*
*/
@Entity
@Table(name = "SALES_PERSON")
@Inheritance(strategy =InheritanceType.SINGLE_TABLE)//指定继承关系的生成策略
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)//指定生成新的判断对象类型的字段的名称和类型
@DiscriminatorValue("person")//DiscriminatorColumn的值
public class Person {
private Integer id;
private String name;
@Id
@Column(name = "id")
@SequenceGenerator(name = "SALES_PERSON_ID", sequenceName = "SALES_PERSON_ID", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SALES_PERSON_ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.java
package com.xiaoxiao.model;
import javax.persistence.*;
@Entity
@DiscriminatorValue("student")
public class Student extends Person {
private Integer score;
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
}
Teacher.java
package com.xiaoxiao.model;
import javax.persistence.*;
@Entity
@DiscriminatorValue("teacher")
public class Teacher extends Person {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
生成的数据库表如下 存入数据后如下
TABLE_PER_CLASS:是为每一个类创建一个表,这些表示相互独立的。
JOINED:是将父类和子类分别存放在不同表中,并且建立相应的外键,以确定相互之间的关系。
- 二.使用配置,继承映射写法
SINGLE_TABLE
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xiaoxiao.pojo.People" table="PEOPLE">
<id name="id" type="string">
<column name="ID"></column>
<generator class="uuid"></generator>
</id>
<discriminator column="PEOPLETYPE" type="string"></discriminator>
<property name="name" column="NAME" type="string"></property>
<property name="sex" column="SEX" type="string"></property>
<property name="age" column="AGE" type="string"></property>
<property name="birthday" column="BIRTHDAY" type="timestamp"></property>
<subclass name="com.xiaoxiao.pojo.Student"discriminator-value="student">
<property name="cardId" column="CARDID" type="string"></property>
</subclass>
<subclass name="com.xiaoxiao.pojo.Teacher" discriminator-value="teacher">
<property name="salary" column="SALARY" type="string"></property>
</subclass>
</class>
</hibernate-mapping>
TABLE_PER_CLASS
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xiaoxiao.pojo.People" abstract="true">//<class>标签中的"abstract"属性如果值为true则,不会生成表结构。如果值为false则会生成表结构,但是不会插入数据
<id name="id" type="string">
<column name="ID"></column>
<generator class="uuid"></generator>
</id>
<property name="name" column="NAME" type="string"></property>
<property name="sex" column="SEX" type="string"></property>
<property name="age" column="AGE" type="string"></property>
<property name="birthday" column="BIRTHDAY" type="timestamp"></property>
<!--
<union-subclass name="com.xiaoxiao.pojo.Student" table="STUDENT">
<property name="cardId" column="CARDID" type="string"></property>
</union-subclass>
<union-subclass name="com.xiaoxiao.pojo.Teacher" table="TEACHER">
<property name="salary" column="SALARY" type="integer"></property>
</union-subclass>
-->
</class>
<!-- 可单独写在Student.hbm.xml里 -->
<union-subclass name="com.xiaoxiao.pojo.Student"
table="STUDENT" extends="com.xiaoxiao.pojo.People">
<property name="cardId" column="CARDID" type="string"></property>
</union-subclass>
<!-- 可单独写在Teacher.hbm.xml里 -->
<union-subclass name="com.xiaoxiao.pojo.Teacher"
table="TEACHER" extends="com.xiaoxiao.pojo.People">
<property name="salary" column="SALARY" type="integer"></property>
</union-subclass>
</hibernate-mapping>
JOINED
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xiaoxiao.pojo.People" table="PEOPLE">
<id name="id" type="string">
<column name="ID"></column>
<generator class="uuid"></generator>
</id>
<property name="name" column="NAME" type="string"></property>
<property name="sex" column="SEX" type="string"></property>
<property name="age" column="AGE" type="string"></property>
<property name="birthday" column="BIRTHDAY" type="timestamp"></property>
<joined-subclass name="com.xiaoxiao.pojo.Student" table="STUDENT">
<key column="ID"></key>
<property name="cardId" column="CARDID" type="string"></property>
</joined-subclass>
<joined-subclass name="com.xiaoxiao.pojo.Teacher" table="TEACHER">
<key column="ID"></key>
<property name="salary" column="SALARY" type="integer"></property>
</joined-subclass>
</class>
</hibernate-mapping>