通过这种继承策略,我们可以通过单表映射整个层次结构。 这里,在表中创建一个额外的列(也称为discriminator
列)来标识该类。
让我们先了解问题。下面给出的整个层次类映射到数据库的一个表中图解说明。
这个层次结构中有三个类。Employee
是Regular_Employee
和Contract_Employee
类的超类(父类)。 让我们来看看这个层次结构类的映射文件。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zaixian.Employee" table="emp121"
discriminator-value="emp">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator column="type" type="string"></discriminator>
<property name="name"></property>
<subclass name="com.zaixian.Regular_Employee"
discriminator-value="reg_emp">
<property name="salary"></property>
<property name="bonus"></property>
</subclass>
<subclass name="com.zaixian.Contract_Employee"
discriminator-value="con_emp">
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</subclass>
</class>
</hibernate-mapping>
在每个类层次结构一个表的情况下,hibernate框架添加了一个标识符列,该框架指定了记录的类型。 它主要用于区分记录。要指定它,必须指定类的 discriminator
子元素。
类的子类subelement
指定子类。 在这种情况下,Regular_Employee
和Contract_Employee
是Employee
类的子类。
此层次结构的表结构如下所示:
列名 | 数据类型 | 是否为空 | 默认值 | 是否主键 |
---|---|---|---|---|
id | int(10) | 否 | - | 是 |
type | varchar(254) | 否 | - | - |
name | varchar(254) | 是 | - | - |
salary | float | 是 | - | - |
bonus | int(10) | 是 | - | - |
pay_per_hour | float | 是 | - | - |
contract_duration | 是 | - | - |
每个类层次结构一个表示例
在这个例子中,我们创建了三个类,并在employee.hbm.xml
文件中提供了这些类的映射。
完整的工程目录如下所示 -
下载代码的项目名称:table-per-hierarchy-example-using-xml-file
1)创建持久类
您需要创建表示继承的持久化类。让我们为上面的层次结构创建三个类:
文件: Employee.java
package com.zaixian;
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
文件: Regular_Employee.java
package com.zaixian;
public class Regular_Employee extends Employee {
private float salary;
private int bonus;
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
文件: Contract_Employee.java
package com.zaixian;
public class Contract_Employee extends Employee {
private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float payPerHour) {
pay_per_hour = payPerHour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contractDuration) {
contract_duration = contractDuration;
}
}
2)创建持久化类的映射文件
上面我们已经了解了层次结构,下面来看看如何配置映射文件。
文件: employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zaixian.Employee" table="emp121"
discriminator-value="emp">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator column="type" type="string"></discriminator>
<property name="name"></property>
<subclass name="com.zaixian.Regular_Employee"
discriminator-value="reg_emp">
<property name="salary"></property>
<property name="bonus"></property>
</subclass>
<subclass name="com.zaixian.Contract_Employee"
discriminator-value="con_emp">
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</subclass>
</class>
</hibernate-mapping>
3)在配置文件中添加hbm文件的映射
打开hibernate.cfg.xml
文件,并添加如下映射资源的条目:
<mapping resource="employee.hbm.xml"/>
现在配置文件将如下所示:
文件:hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hbm2ddl.auto
属性定义是用于在数据库中创建自动表。
4)创建存储持久对象的类
在这个类中,我们只是将Employee
对象存储到数据库表中。
文件:StoreData.java
package com.zaixian;
import org.hibernate.*;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.*;
public class StoreData {
public static void main(String[] args) {
// Session session = new
// AnnotationConfiguration().configure().buildSessionFactory().openSession();
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
// 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
SessionFactory sessionFactory = new MetadataSources(registry)
.buildMetadata().buildSessionFactory();
/**** 上面是配置准备,下面开始我们的数据库操作 ******/
Session session = sessionFactory.openSession();// 从会话工厂获取一个session
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("zaixian");
Regular_Employee e2 = new Regular_Employee();
e2.setName("Max su");
e2.setSalary(50000);
e2.setBonus(5);
Contract_Employee e3 = new Contract_Employee();
e3.setName("Hippo su");
e3.setPay_per_hour(1000);
e3.setContract_duration("15 hours");
session.persist(e1);
session.persist(e2);
session.persist(e3);
t.commit();
session.close();
System.out.println("success");
}
}
运行上面程序,并打数据库表查看执行结果。