当前位置:网站首页>Class table inheritance pattern

Class table inheritance pattern

2021-05-04 14:01:16 Jiedao jdon

This schema belongs to the object relational structure schema Directory , This directory belongs to Enterprise application architecture pattern .

Purpose

Represents the inheritance hierarchy of a class , Each class has a table . Class table inheritance Support a database table for each class in the inheritance structure .

explain

In the picture , Each class has its own database table .

Player Class has its own player database table .

Cricketer Class has its own cricketer database table .

Footballer Class has its own football player database table .

Bowler Class has its own bowling database table .

How to operate

About class table inheritance , The most direct point is in the domain model , Each class has a table . The fields in the domain class map directly to the fields in the corresponding table . Like other inheritance mappings , The basic method of inheritance mapper also applies .

Class table inheritance The biggest implementation problem of is how to return data from multiple tables in an efficient way . Due to multiple calls to the database , Obviously calling each table is not good . You can avoid this by connecting across various component tables ;  however , Because of the way the database is optimized , Connections with more than three or four tables are often slow . 

When to use it

Class table inheritance , Single table inheritance and concrete table inheritance (Class Table Inheritance, Single Table Inheritance, and Concrete Table Inheritance ) There are three ways to consider inheritance mapping .

The advantage of class table inheritance is :

  • All columns are related to each row , So the form is easier to understand , No waste of space .

  • The relationship between the domain model and the database is very simple .

The weakness of class table inheritance is :

  • You need to touch multiple tables to load objects , This means that one connection or multiple queries are consolidated in memory .

  • Any refactoring of fields up and down the hierarchy will result in database changes .

  • The parent type table can become a bottleneck , Because they have to be visited all the time .

  • A high degree of normalization may make ad hoc queries difficult to understand .

  • You don't have to choose an inheritance mapping pattern for a class hierarchy . You can use class table inheritance for the classes at the top of the hierarchy ,  Use a specific set of table inheritance for the lower tier (Concrete Table Inheritance).

perform

Hibernate ORM Framework Provides inheritance mapping strategy :Table Per Concrete Class uses Annotation

For each specific class table , Create tables from classes . But duplicate columns are added to the subclass table .

about Table Per Concrete class , Each class creates a table . So there are no nullable values in the table . The disadvantage of this method is that duplicate columns are created in the subclass table .

ad locum , We need to use... In the parent class @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) notes , Use... In subclasses @AttributeOverrides notes .

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)   Specify the table that we use for each specific class policy . It should only be specified in the parent class .

@AttributeOverrides   The definition will override the parent class property in this class . In table structure , The parent table column will be added to the child table .

The class hierarchy is as follows :

establish Hibernate Model class . We need to create persistent classes that represent inheritance . Let's create three classes for the above hierarchy :

import javax.persistence.*;  
  
@Entity  
@Table(name = "employee102")  
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)  
  
public class Employee {  
 @Id  
 @GeneratedValue(strategy=GenerationType.AUTO)  
    
 @Column(name = "id")  
 private int id;  
   
 @Column(name = "name")  
 private String name;  
   
 //setters and getters  
}  

import javax.persistence.*;  
  
@Entity  
@Table(name="regularemployee102")  
@AttributeOverrides({  
    @AttributeOverride(name="id", column=@Column(name="id")),  
    @AttributeOverride(name="name", column=@Column(name="name"))  
})  
public class Regular_Employee extends Employee{  
      
 @Column(name="salary")    
 private float salary;  
   
 @Column(name="bonus")     
 private int bonus;  
   
 //setters and getters  
}  

import javax.persistence.*;  
@Entity  
@Table(name="contractemployee102")  
@AttributeOverrides({  
    @AttributeOverride(name="id", column=@Column(name="id")),  
    @AttributeOverride(name="name", column=@Column(name="name"))  
})  
public class Contract_Employee extends Employee{  
      
    @Column(name="pay_per_hour")  
    private float pay_per_hour;  
      
    @Column(name="contract_duration")  
    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;  
    }  
}  

Add... To the configuration file  hbm  Mapping of files -  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="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">oracle</property>  
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
          
        <mapping class="com.javatpoint.mypackage.Employee"/>  
        <mapping class="com.javatpoint.mypackage.Contract_Employee"/>  
        <mapping class="com.javatpoint.mypackage.Regular_Employee"/>  
    </session-factory>  
</hibernate-configuration>  

Create classes that store persistent objects . In this class , We're just gonna employee Objects are stored in the database .

file :StoreData.java

import org.hibernate.*;  
import org.hibernate.cfg.*;  
  
public class StoreData {  
 public static void main(String[] args) {  
 AnnotationConfiguration cfg=new AnnotationConfiguration();  
 Session session=cfg.configure("hibernate.cfg.xml").buildSessionFactory().openSession();  
    
  Transaction t=session.beginTransaction();  
    
  Employee e1=new Employee();  
  e1.setName("sonoo");  
    
  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");  
 }  
}  

 

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504135746078h.html

随机推荐