例:
Computer类,是抽象类,是父类
public abstract class Computer{
private int id;
private int price;
private String manufacturer;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getManufacturer(){
return manufacturer;
}
public void setManufacturer(String manufacturer){
this.manufacturer = manufacturer;
}
public int getPrice(){
return price;
}
public void setPrice(int price){
this.price = price;
}
}
子类台式机 Desktop类
public class Desktop extends Computer{
private boolean isLCD;
public boolean isLCD(){
return isLCD;
}
public void setLCD(boolean isLCD){
this.isLCD = isLCD;
}
}
子类笔记本电脑 Notepad类
public class Notepad extends Computer{
private float weight;
private float thickness;
public float getThickness(){
return thickness;
}
public void setThickness(float thickness){
this.thickness = thickness;
}
public float getWeight(){
return weight;
}
public void setWeight(float weight){
this.weight = weight;
}
}
所有类建一个表的映射文件
<hibernate-mapping package="alan.hbn.inheritance.entity">
<class name="Computer" table="computer_tph" discriminator-value="c">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<discriminator column="category" type="character" />
<!--用来表识父子类的标记字段-->
<property name="price" column="price" type="integer" not-null="true"/>
<property name="manufacturer" column="manufacturer" type="string" length="30" not-null="true"/>
<!--subclass是定义子类中特有部分的标签-->
<subclass name="Desktop" discriminator-value="d">
<!--discriminator-value属性是指定本类的标识符的-->
<property name="LCD" column="islcd" type="yes_no" />
<!--
指定子类的特有属性 type=”yes_no”是把布尔值转成单字节的存储数据库的类型,如果为true就会在字段中写入“Y”,反之为“N”
-->
</subclass>
<subclass name="Notepad" discriminator-value="n">
<property name="weight" column="weight" type="float" />
<property name="thickness" column="thickness" type="float" />
</subclass>
</class>
</hibernate-mapping>
只为具体类建表的配置文件,还可以将这两个类的配置文件分开来写,这样就可单表的映射文件没有区别了。
<hibernate-mapping package="alan.hbn.inheritance.entity">
<class name="Computer" abstract="true">
<id name="id" column="id" type="integer">
<generator class="increment" />
</id>
<property name="price" column="price" type="integer" not-null="true"/>
<property name="manufacturer" column="manufacturer" type="string" length="30" not-null="true"/>
<!--union-subcalss标签是指定子类对应的表和子类特有的属性-->
<union-subclass name="Desktop" table="desktop">
<property name="LCD" column="islcd" type="yes_no" />
</union-subclass>
<union-subclass name="Notepad" table="notepad">
<property name="weight" column="weight" type="float" />
<property name="thickness" column="thickness" type="float" />
</union-subclass>
</class>
</hibernate-mapping>
每个类建一格表的配置文件
<hibernate-mapping package="alan.hbn.inheritance.entity">
<class name="Computer" table="computer_tpc">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="price" column="price" type="integer" not-null="true"/>
<property name="manufacturer" column="manufacturer" type="string" length="30" not-null="true"/>
<!--union-subcalss标签是指定子类对应的表和子类特有的属性-->
<joined-subclass name="Desktop" table="desktop_tpc">
<key column="computerid" />
<!--指定使用父类的Id生成策略-->
<property name="LCD" column="islcd" type="yes_no" />
</joined-subclass>
<joined-subclass name="Notepad" table="notepad_tpc">
<key column="computerid" />
<property name="weight" column="weight" type="float" />
<property name="thickness" column="thickness" type="float" />
</joined-subclass>
</class>
</hibernate-mapping>