Hibernate教程-使用XML文件实现每个子类的表
![](https://wx.javatiku.cn/j/j301-t.jpg)
在每个子类的表模式中,子类映射的表通过主键和外键关系与父类映射的表相关联。
<joined-subclass> 元素用于通过主键和外键关系将子类映射到父类。
在这个示例中,我们将使用 hb2ddl.auto 属性自动生成表。因此,我们不需要担心在数据库中创建表。
让我们看一下将要映射的类的层次结构。
让我们看一下如何通过 joined-subclass
元素映射此层次结构:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
<hibernate-mapping>
<class name="cn.javatiku.mypackage.Employee" table="emp123">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<joined-subclass name="cn.javatiku.mypackage.Regular_Employee" table="regemp123">
<key column="eid"></key>
<property name="salary"></property>
<property name="bonus"></property>
</joined-subclass>
<joined-subclass name="cn.javatiku.mypackage.Contract_Employee" table="contemp123">
<key column="eid"></key>
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</joined-subclass>
</class>
</hibernate-mapping>
在每个子类的表模式中,数据库中将有三个表,每个表代表一个特定的类。
joined-subclass 子元素指定子类。
key 子元素用于在子类映射表中生成外键。此外键将与父类映射表的主键相关联。
每个表的表结构如下:
Employee 类的表结构
Regular_Employee 类的表结构
Contract_Employee 类的表结构
每个子类的表示例
在这个示例中,我们将创建三个类,并在 employee.hbm.xml 文件中提供这些类的映射。
1) 创建持久化类
你需要创建表示继承关系的持久化类。让我们为上述层次结构创建三个类:
文件: Employee.java
package cn.javatiku.mypackage;
public class Employee {
private int id;
private String name;
// getters and setters
}
文件: Regular_Employee.java
package cn.javatiku.mypackage;
public class Regular_Employee extends Employee {
private float salary;
private int bonus;
// getters and setters
}
文件: Contract_Employee.java
package cn.javatiku.mypackage;
public class Contract_Employee extends Employee {
private float pay_per_hour;
private String contract_duration;
// getters and setters
}
2) 创建持久化类的映射文件
上述层次结构的映射已经讨论过了。
文件: employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
<hibernate-mapping>
<class name="cn.javatiku.mypackage.Employee" table="emp123">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property>
<joined-subclass name="cn.javatiku.mypackage.Regular_Employee" table="regemp123">
<key column="eid"></key>
<property name="salary"></property>
<property name="bonus"></property>
</joined-subclass>
<joined-subclass name="cn.javatiku.mypackage.Contract_Employee" table="contemp123">
<key column="eid"></key>
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</joined-subclass>
</class>
</hibernate-mapping>
3) 创建配置文件
打开 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 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">jtp</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hbm2ddl.auto 属性定义用于在数据库中自动创建表。
4) 创建存储持久化对象的类
在这个类中,我们将简单地将员工对象存储在数据库中。
文件: StoreData.java
package cn.javatiku.mypackage;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class StoreData {
public static void main(String[] args) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("Gaurav Chawla");
Regular_Employee e2 = new Regular_Employee();
e2.setName("Vivek Kumar");
e2.setSalary(50000);
e2.setBonus(5);
Contract_Employee e3 = new Contract_Employee();
e3.setName("Arjun Kumar");
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");
}
}
运行 StoreData
类将三个员工对象持久化到数据库中,各自存储在对应的表中。