父类:电脑(Computer)
子类:笔记本(Notebook)、台式机(PC)
[img]
[/img]
实体类:
电脑类
package entity;
public class Computer {
private int oid;
private String logo;
private double price;
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
笔记本类
package entity;
public class NoteBook extends Computer {
private double duration;
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
}
台式机类
package entity;
public class PC extends Computer {
private double component;
public double getComponent() {
return component;
}
public void setComponent(double component) {
this.component = component;
}
}
1)所有字段(属性)放在同一张表
需要用到得关键字<descriminator> <subclass>
映射文件Computer.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="entity">
<!-- 继承映射,方案一,所有对象属性存放在同一张表 -->
<class name="Computer" table="t_computer">
<id name="oid" column="computer_id">
<generator class="native"></generator>
</id>
<!-- 不同记录用鉴别字段来区分类型 -->
<discriminator column="computer_type"></discriminator>
<property name="logo"></property>
<property name="price"></property>
<subclass name="PC" discriminator-value="P">
<property name="component"></property>
</subclass>
</class>
<!-- subclass 写在class 外面要用关键字extends="父类名">
<subclass name="NoteBook" extends="Computer" discriminator-value="N">
<property name="duration"></duration>
</subclass>
</hibernate-mapping>
2)每个类各自一张表
要用到得关键字 <joined-subclass>
映射文件Computer.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="entity">
<!-- 继承映射,方案二,所有对象各自存放一张表 -->
<class name="Computer" table="t_computer">
<id name="oid" column="computer_id">
<generator class="native"></generator>
</id>
<property name="logo"></property>
<property name="price"></property>
</class>
<joined-subclass name="PC" extends="Computer"
table="t_pc" >
<key column="computer_id"></key>
<property name="component"></property>
</joined-subclass>
<joined-subclass name="NoteBook" extends="Computer"
table="t_notebook">
<key column="computer_id"></key>
<property name="duration"></property>
</joined-subclass>
</hibernate-mapping>
3)每个具体类各自一张表,没有父类表
要用到的关键字<union-subclass>注意主键id不能重复
<class abstract="true"> -->不建父类表
映射文件Computer.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="entity">
<!-- 继承映射,方案三,每个具体子类各自存放一张表 -->
<class name="Computer" abstract="true">
<id name="oid" column="computer_id">
<generator class="assigned"></generator>
</id>
<property name="logo"></property>
<property name="price"></property>
</class>
<union-subclass name="PC" extends="Computer"
table="t_pc" >
<property name="component"></property>
</union-subclass>
<union-subclass name="NoteBook" extends="Computer"
table="t_notebook">
<property name="duration"></property>
</union-subclass></hibernate-mapping>