37
技術社區[雲棲]
第九章 關係映射 繼承關聯映射
1、discriminator鑒別器
同一張表中表示不同的類型

在employee表中定義一個type字段,表示員工的類型
name是所有的員工都有的屬性。
sale_count是銷售員特有的屬性。
而skiller是技術人員特有的屬性。
三個實體類:
Employee:
package cn.framelife.mvc.entity;
import java.io.Serializable;
public class Employee implements Serializable {
private Integer id;
private String name;
public Employee() {
}
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;
}
}
Sales:
package cn.framelife.mvc.entity;
public class Sales extends Employee {
private Integer saleCount;
public Integer getSaleCount() {
return saleCount;
}
public void setSaleCount(Integer saleCount) {
this.saleCount = saleCount;
}
}
Skiller:
package cn.framelife.mvc.entity;
public class Skiller extends Employee {
private String skiller;
public String getSkiller() {
return skiller;
}
public void setSkiller(String skiller) {
this.skiller = skiller;
}
}
Employee.hbm.xml:
<hibernate-mapping>
<!-- discriminator-value="0"鑒別器父類Employee默認的類型為0 -->
<class name="cn.framelife.hibernate.entity.Employee" table="employee" catalog="hibernate" discriminator-value="0">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator />
</id>
<!-- 配置鑒別器,配置表中的類型字段 -->
<discriminator column="type"></discriminator>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
<!-- 配置Employee的子類,並且設定類型的值 discriminator-value-->
<subclass name="cn.framelife.hibernate.entity.Sales" discriminator-value="1">
<property name="saleCount" column="sale_count"></property>
</subclass>
<subclass name="cn.framelife.hibernate.entity.Skiller" discriminator-value="2">
<property name="skiller" column="skiller"></property>
</subclass>
</class>
</hibernate-mapping>
增加操作:
tx = session.beginTransaction();
//1、增加一個Employee
// Employee employee = new Employee();
// employee.setName("a");
// session.save(employee);
//2、增加一個Skiller
// Skiller skiller = new Skiller();
// skiller.setName("b");
// skiller.setSkiller("kill somebody");
// session.save(skiller);
//3、增加一個Sales
Sales sales = new Sales();
sales.setName("c");
sales.setSaleCount(100);
session.save(sales);
tx.commit();
2、joined-subclass內連接
一張表表示一種類型



實體類與discriminator鑒別器的一樣。
Employee.hbm.xml:
<hibernate-mapping> <class name="cn.framelife.hibernate.entity.Employee" table="employee" catalog="hibernate"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator /> </id> <property name="name" type="java.lang.String"> <column name="name" length="45" not-null="true" /> </property> <!-- 這裏是關鍵配置 --> <joined-subclass name="cn.framelife.hibernate.entity.Skiller" table="skiller"> <key column="id"></key> <property name="skiller" column="skiller"></property> </joined-subclass> <joined-subclass name="cn.framelife.hibernate.entity.Sales" table="sales"> <key column="id"></key> <property name="saleCount" column="sale_count"></property> </joined-subclass> </class> </hibernate-mapping>
增加操作與discriminator鑒別器的一樣。
3、union-subclass
這種方法在表設計的時候不需要employee表,隻需要子表。


實體類與discriminator鑒別器的一樣。
Employee.hbm.xml:
<class name="cn.framelife.hibernate.entity.Employee" catalog="hibernate"> <!-- 這裏的id生成策略不能使用數據庫自增長(indentity,或者是像native,如果你使用的是數據庫自增長的話).可以使用hilo或者increment --> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator /> </id> <property name="name" type="java.lang.String"> <column name="name" length="45" not-null="true" /> </property> <!-- 這裏是關鍵配置 --> <union-subclass name="cn.framelife.hibernate.entity.Skiller" table="skiller"> <property name="skiller" column="skiller"></property> </union-subclass> <union-subclass name="cn.framelife.hibernate.entity.Sales" table="sales"> <property name="saleCount" column="sale_count"></property> </union-subclass> </class> </hibernate-mapping>
增加操作與discriminator鑒別器的一樣。
最後更新:2017-04-03 18:52:09