一、继承关系之整个继承树映射到一张表
实体类:
package com.cos.entity;
public class Skiller extends Employee{
private String skill;
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
}
/
package com.cos.entity;
public class Sales extends Employee{
private int sell;
public int getSell() {
return sell;
}
public void setSell(int sell) {
this.sell = sell;
}
}
配置文件:Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cos.entity"> <class name="Employee" discriminator-value="0"> <id name="id"> <generator class="native"/> </id> <discriminator column="type" type="int"/> <property name="name" column="name"/> <subclass name="Skiller" discriminator-value="1"> <property name="skill"/> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="sell"/> </subclass> </class> </hibernate-mapping>
说明:
<subclass>说明的是子类。name说明的是子类类名。
<property>说明的是子类里的字段。
<discriminator>鉴别器字段。配置的是子类的类型,表字段为type,字段的值为int型。
discriminator-value配置子类类型的值。
二、继承关系之每个类映射到一张表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cos.entity"> <class name="Employee" discriminator-value="0"> <id name="id"> <generator class="native"/> </id> <property name="name" column="name"/> <joined-subclass name="Skiller" table="skiller"> <key column="emp_id"/> <property name="skill"/> </joined-subclass> <joined-subclass name="Sales" table="sales"> <key column="emp_id"/> <property name="sell"/> </joined-subclass> </class> </hibernate-mapping>
其中:
<joined-subclass>表示子类自成一张表,子类的名字用name表示,子类对应的表用table表示。
<key>表示
<property>表示子类的字段。
三、继承关系之每个具体类映射一张独立表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.cos.entity"> <class name="Employee"> <id name="id"> <generator class="hilo"/> </id> <property name="name" column="name"/> <union-subclass name="Skiller" table="skiller"> <property name="skill"/> </union-subclass> <union-subclass name="Sales" table="sales"> <property name="sell"/> </union-subclass> </class> </hibernate-mapping>
<union-subclass>表示每个具体子类映射一张独立表。
<generator class="hilo"/>表示使用高地位的方式生成主键。