在表按具体类的情况下,数据库中将有三张没有相互关系的表。映射表和表按具体类策略有两种方法:

  • 通过 union-subclass 元素
  • 通过为每个类自行创建表

让我们理解我们将要映射的层次结构。

4-1.jpg

让我们看看如何通过 union-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="emp122"> 
    <id name="id"> 
        <generator class="increment"></generator> 
    </id> 
    <property name="name"></property> 
    <union-subclass name="cn.javatiku.mypackage.Regular_Employee" table="regemp122"> 
        <property name="salary"></property> 
        <property name="bonus"></property> 
    </union-subclass> 
    <union-subclass name="cn.javatiku.mypackage.Contract_Employee" table="contemp122"> 
        <property name="pay_per_hour"></property> 
        <property name="contract_duration"></property> 
    </union-subclass> 
</class> 
</hibernate-mapping> 

在表按具体类的情况下,数据库中将有三张表,每张表代表一个特定的类。

union-subclass 类的子元素指定子类。它将父表的列添加到这个表中。换句话说,它是作为一个联合操作。

每个表的表结构如下:

Employee 类的表结构

4-2.jpg

Regular_Employee 类的表结构

4-3.jpg

Contract_Employee 类的表结构

4-4.jpg

表按具体类的示例

在这个示例中,我们创建了三个类,并在 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="emp122"> 
    <id name="id"> 
        <generator class="increment"></generator> 
    </id> 
    <property name="name"></property> 
    <union-subclass name="cn.javatiku.mypackage.Regular_Employee" table="regemp122"> 
        <property name="salary"></property> 
        <property name="bonus"></property> 
    </union-subclass> 
    <union-subclass name="cn.javatiku.mypackage.Contract_Employee" table="contemp122"> 
        <property name="pay_per_hour"></property> 
        <property name="contract_duration"></property> 
    </union-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 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.*;
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.getSessionFactoryBuilder().build();
        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");
    }
}

标签: Hibernate, Hibernate教程, Hibernate框架, Hibernate框架设计, Hibernate初级教程, Hibernate框架用法, Hibernate指南, Hibernate入门, Hibernate中级教程, Hibernate进阶教程, Hibernate高级教程, Hibernate下载