- 一.使用註解,繼承映射寫法
繼承映射在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>