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 class="native" />
</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 class="native" />
</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 class="increment" />
</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鉴别器的一样。