📜  Hibernate – 使用 XML 文件的每个子类的表示例

📅  最后修改于: 2022-05-13 01:55:05.001000             🧑  作者: Mango

Hibernate – 使用 XML 文件的每个子类的表示例

Table Per Subclass中,子类表通过主键外键关系映射到 Parent 类表。在每个子类策略的表中:

  • 对于每一类层次结构,数据库中都存在一个单独的表。
  • 在创建数据库表时,需要在父表和子表之间建立外键关系。
  • 为了将信息传递给应用每个子类映射的表的hibernate,我们在hbm.xml文件的标记下配置标记。
  • 鉴别器列是可选的。

由于子类表与超类表有外键关联,因此子类表中不会有任何重复的列,除了需要通过外键维护父类表和子类表之间关系的一列。

Table Per Subclass 策略示例(XML 映射)

假设我们有一个Employee类,其子类为P_Employee 和 C_Employee。以下是这些类的类图和关系。

类的层次结构

类的层次结构

我们有 3 个表EmployeeP_Employee C_Employee来保存类数据。子类表和超类表之间存在外键关系。因此,公共数据存储在Employee表中,子类特定字段存储在P_EmployeeC_Employee表中。

创建数据库表以持久化具体类:

XML 映射的项目结构(IntelliJ IDEA):

为上述层次结构创建 Employee、P_Employee 和 C_Employee 类:

下面是Employee的实现。 Java文件:

Java
package com.exploit.model;
  
public class Employee {
    private int id;
    private String name;
    private int age;
  
    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; }
  
    public int getAge() { return age; }
  
    public void setAge(int age) { this.age = age; }
}


Java
package com.exploit.model;
  
public class P_Employee extends Employee {
    private double salary;
  
    public double getSalary() { return salary; }
  
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
}


Java
package com.exploit.model;
  
public class C_Employee extends Employee {
    private double hourlyRate;
    private double duration;
  
    public double getDuration() { return duration; }
  
    public void setDuration(double duration)
    {
        this.duration = duration;
    }
  
    public double getHourlyRate() { return hourlyRate; }
  
    public void setHourlyRate(double hourlyRate)
    {
        this.hourlyRate = hourlyRate;
    }
}


XML


  

    
        
            
        
        
        
  
        
            
            
        
  
        
            
            
            
        
  
    
  


XML


  

  
    
  
        
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost/javainsimpleway
        root
        root
  
        
        1
  
        
        org.hibernate.dialect.MySQLDialect
  
        
        org.hibernate.cache.internal.NoCacheProvider
  
        
        true
  
        
        true
  
        
        update
  
        
  
    
  


XML

    4.0.0
  
    TablePerSubclassXML
    TablePerSubclassXML
    0.0.1-SNAPSHOT
    jar
  
    TablePerSubclassXML
    http://maven.apache.org
  
    
        UTF-8
    
  
    
        
            junit
            junit
            3.8.1
            test
        
        
        
        
            org.hibernate
            hibernate-core
            5.2.6.Final
        
        
        
        
            mysql
            mysql-connector-java
            6.0.5
        
        
    


Java
package com.exploit.db;
  
import com.exploit.model.C_Employee;
import com.exploit.model.Employee;
import com.exploit.model.P_Employee;
import com.exploit.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
  
public class Main {
    public static void main(String[] args)
    {
        // Get session factory using Hibernate Util class
        SessionFactory sessionFactory
            = HibernateUtil.getSessionFactory();
        
        // Get session from Sesson factory
        Session session = sessionFactory.openSession();
  
        // Begin transaction
        Transaction transaction
            = session.beginTransaction();
  
        // Creating Employee base class record
        Employee employee = new Employee();
        employee.setName("ChikkoRita");
        employee.setAge(19);
  
        // Creating Permanent Employee subclass record
        P_Employee permanentEmployee = new P_Employee();
        permanentEmployee.setName("Saili.H");
        permanentEmployee.setAge(18);
        permanentEmployee.setSalary(30000);
  
        // Creating Contract Employee subclass record
        C_Employee contractEmployee = new C_Employee();
        contractEmployee.setName("KirikoChan");
        contractEmployee.setAge(19);
        contractEmployee.setHourlyRate(2000);
        contractEmployee.setDuration(8.5);
  
        // persist all the employee records
        session.persist(employee);
        session.persist(permanentEmployee);
        session.persist(contractEmployee);
  
        // Commit the transaction and close the session
        transaction.commit();
        session.close();
        System.out.println(
            "Employee records successfully persisted");
    }
}


下面是P_Employee 的实现。 Java文件:

Java

package com.exploit.model;
  
public class P_Employee extends Employee {
    private double salary;
  
    public double getSalary() { return salary; }
  
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
}

下面是 C_Employee 的实现。 Java文件:

Java

package com.exploit.model;
  
public class C_Employee extends Employee {
    private double hourlyRate;
    private double duration;
  
    public double getDuration() { return duration; }
  
    public void setDuration(double duration)
    {
        this.duration = duration;
    }
  
    public double getHourlyRate() { return hourlyRate; }
  
    public void setHourlyRate(double hourlyRate)
    {
        this.hourlyRate = hourlyRate;
    }
}

为 Persistent 类创建映射文件:

下面是employee.hbm.xml文件的实现:

XML



  

    
        
            
        
        
        
  
        
            
            
        
  
        
            
            
            
        
  
    
  

在hibernate配置文件中添加hbm.xml文件的映射:

下面是hibernate.cfg.xml文件的实现:

XML



  

  
    
  
        
        com.mysql.jdbc.Driver
        jdbc:mysql://localhost/javainsimpleway
        root
        root
  
        
        1
  
        
        org.hibernate.dialect.MySQLDialect
  
        
        org.hibernate.cache.internal.NoCacheProvider
  
        
        true
  
        
        true
  
        
        update
  
        
  
    
  

以下是 pom.xml 文件中使用的依赖项:

XML


    4.0.0
  
    TablePerSubclassXML
    TablePerSubclassXML
    0.0.1-SNAPSHOT
    jar
  
    TablePerSubclassXML
    http://maven.apache.org
  
    
        UTF-8
    
  
    
        
            junit
            junit
            3.8.1
            test
        
        
        
        
            org.hibernate
            hibernate-core
            5.2.6.Final
        
        
        
        
            mysql
            mysql-connector-java
            6.0.5
        
        
    

创建存储持久对象的类:

下面是Main 的实现。 Java文件:

Java

package com.exploit.db;
  
import com.exploit.model.C_Employee;
import com.exploit.model.Employee;
import com.exploit.model.P_Employee;
import com.exploit.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
  
public class Main {
    public static void main(String[] args)
    {
        // Get session factory using Hibernate Util class
        SessionFactory sessionFactory
            = HibernateUtil.getSessionFactory();
        
        // Get session from Sesson factory
        Session session = sessionFactory.openSession();
  
        // Begin transaction
        Transaction transaction
            = session.beginTransaction();
  
        // Creating Employee base class record
        Employee employee = new Employee();
        employee.setName("ChikkoRita");
        employee.setAge(19);
  
        // Creating Permanent Employee subclass record
        P_Employee permanentEmployee = new P_Employee();
        permanentEmployee.setName("Saili.H");
        permanentEmployee.setAge(18);
        permanentEmployee.setSalary(30000);
  
        // Creating Contract Employee subclass record
        C_Employee contractEmployee = new C_Employee();
        contractEmployee.setName("KirikoChan");
        contractEmployee.setAge(19);
        contractEmployee.setHourlyRate(2000);
        contractEmployee.setDuration(8.5);
  
        // persist all the employee records
        session.persist(employee);
        session.persist(permanentEmployee);
        session.persist(contractEmployee);
  
        // Commit the transaction and close the session
        transaction.commit();
        session.close();
        System.out.println(
            "Employee records successfully persisted");
    }
}

我们只定义了一个休眠映射 (hbm) 文件Employee.hbm.xmlC_EmployeeP_Employee模型类都在一个hbm.xml文件中定义。这是使用XML映射Table Per Subclass的常用方法。