单表继承:每棵类继承树使用一个表
具体表继承:每个子类一个表
类表继承:每个具体类一个表
为三个类建立一张表:
Animal类:
package com.jzq.hibernate;
public class Animal {
private int id;
private String name;
private String sex;
public Animal(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Bird类:
package com.jzq.hibernate;
public class Bird extends Animal {
private int height;
public Bird(){}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
Pig类:
package com.jzq.hibernate;
public class Pig extends Animal {
private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Pig(){}
}
配置文件:
<!--
建立一张表字段包括:id,name,sex,weight,height,type
-->
<hibernate-mapping>
<class name="Animal" package="com.jzq.hibernate">
<id name="id">
<generator class="native"/>
</id>
<!-- 鉴别器,在表中添加字段区分表里的子类对象 -->
<discriminator column="type" type="string"/>
<property name="name"/>
<property name="sex"/>
<!-- 子类pig -->
<subclass name="Pig" discriminator-value="P">
<property name="weight"/>
</subclass>
<!-- 子类bird -->
<subclass name="bird">
<property name="height" discriminator-value="B"/>
</subclass>
</class>
</hibernate-mapping>