In the object-oriented programming field, classes and classes have an inheritance relationship. For example, in the Java World, only the extends keyword is required to determine the parent-child relationship of these two classes, however, in the world of relational databases, there is no keyword between tables to clearly specify the parent-child relationship between the two tables. There is no inheritance relationship between the tables. To reflect the inheritance relationships in the application field to the data, Hibernate provides us with three solutions:
Solution 1: A subclass corresponds to a table.
Solution 2: Use a table to indicate the Union of attributes of classes under all inheritance systems.
Solution 3: each subclass uses a table to store only its unique attributes, and then associates it with the table corresponding to the parent class in a one-to-one primary key Association mode.
The three solutions are in the official language:
TPS: one table (per subclass) for each subclass ).
TPH: each class inheritance tree uses a table (table per class hierarchy)
TPC: class Table inheritance. Each specific class is a table (table per concrete class) (with some restrictions)
Now let's take a look at the advantages and disadvantages of the three solutions based on an example. Let's get familiar with these three solutions. Suppose there are three classes: People, student, and teacher. The parent class is people, and student and teacher are people. The Code is as follows:
People class:
Public class people {/* attributes of the parent class */private stringid; private stringname; private stringsex; private stringage; private timestampbirthday;/* Get and Set Methods */}
Student class:
Public class student extends people {/* attributes unique to students */private string cardid; // student ID Public String getcardid () {return cardid;} public void setcardid (string cardid) {This. cardid = cardid ;}}
Teacher class:
Public class teacher extends people {/* attributes unique to teacher */privateint salary; // salary public int getsalary () {return salary;} public void setsalary (INT salary) {This. salary = salary ;}}
Solution 1: A subclass corresponds to a table(TPS)
This solution enables each subclass in the inheritance system to correspond to a table in the database. As follows:
The database table corresponding to each subclass contains information about the parent class and its unique attributes. Each subclass corresponds to a table, and the table information is complete, that is, it contains all attribute ing fields inherited from the parent class. This policy uses the <Union-subclass> label to define sub-classes.
Configure the people. HBM. xml file:
<?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">
The above configuration is the configuration of a sub-class Table scheme. The <Union-subclass> label is a sub-class used to indicate the class represented by the HBM file. For example, the people class has two sub-classes, two <Union-subclass> labels are required, and so on. <Union-subclass> the "name" attribute of a tag is the fully qualified name of the sub-class. The "table" attribute is used to specify the name of the table corresponding to the sub-class, the "extends" attribute is used to specify the parent class of this subclass. Note that this attribute is related to the location of the <Union-subclass> label, if the <Union-subclass> label is a sub-tag of the <class> label, you can leave the "extends" attribute Unspecified. Otherwise, you must explicitly set the "extends" attribute. If the value of the "abstract" attribute in the <class> label is true, the table structure is not generated. If the value is false, the table structure is generated, but data is not inserted.
Generate the table structure based on people. HBM. xml. You can see that a subclass corresponds to a table:
drop table if exists student drop table if exists teacher create table student ( id varchar(255) not null, name varchar(255), sex varchar(255), age varchar(255), birthday datetime, cardId varchar(255), primary key (id) ) create table teacher ( id varchar(255) not null, name varchar(255), sex varchar(255), age varchar(255), birthday datetime, salary integer, primary key (id) )
Solution 2: Use a table to indicate the Union (TPH) of the attributes of all classes under the Inheritance System)
This policy is implemented using the <subclass> label. Because there are many sub-classes in the class inheritance system, to store the information of multiple classes in a table, there must be a mechanism to distinguish which records belong to which classes. In hibernate, this mechanism adds a field to the table and uses the value of this field for differentiation. Add this label column to the table using the <discriminator> label.
Policy:
After all the class information in the inheritance system is displayed in the same table, null is automatically assigned as long as the class does not have any attribute.
Configure people. 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"> <Discriminator> A label is used to create an ID column in a table. Its "column" attribute specifies the name of the column to be identified, and "type" specifies the type of the column to be identified. <Subclass> A tag is used to determine the subclass of the class in which the HBM file represents. The "name" attribute specifies the name of the subclass, the "discriminator-value" attribute specifies the value of the ID column of the data of this subclass. Its "extends" attribute is consistent with the "extends" attribute usage of <Union-subclass>.
Let's take a look at the table structure generated based on people. HBM. xml. We can see that a table contains all the information in the inheritance system. "leletype" is the identifier column:
drop table if exists people createtable people ( idvarchar(255) not null, peopleType varchar(255) not null, namevarchar(255), sexvarchar(255), agevarchar(255), birthday datetime, cardIdvarchar(255), salaryvarchar(255), primary key (id) )
Solution 3: each subclass uses a table to store only its unique attributes, and then associates it with the table corresponding to the parent class in a one-to-one primary key Association mode. (TPC)
This policy uses the <joined-subclass> label to define sub-classes. The parent class and Child class correspond to a database table. In the database table corresponding to the parent class, it stores the public information of all records. In fact, the table corresponding to the parent class contains all records, including records of the parent class and subclass; in the database table corresponding to the subclass, this table only defines the specific attribute ing fields in the subclass. The data table corresponding to the subclass and the data table corresponding to the parent class are joined by one-to-one primary key Association.
For this policy:
All records of sub-classes are stored in the people table, but only the information they share is recorded. Their unique information is stored in their corresponding table. A record needs to obtain its unique information, you need to use the primary key of the People record to find the record with the same primary key value in its corresponding sub-table, and then retrieve its unique information.
Configure people. 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"> The <joined-subclass> tag must contain a key tag, which specifies the field used to associate the subclass with the parent class.
According to people. HBM. XML generates the table structure. You can see that the table corresponding to the parent class stores public information, and the table corresponding to the Child class stores exclusive information. The table corresponding to the Child class is associated with the parent class using a one-to-one primary key Association.
drop table if exists people drop table if exists student droptable if exists teacher create table people ( id varchar(255) not null, name varchar(255), sex varchar(255), age varchar(255), birthday datetime, primary key (id) ) create table student ( id varchar(255) not null, cardId varchar(255), primary key (id) ) create table teacher ( id varchar(255) not null, salary integer, primary key (id) ) alter table student add index FK8FFE823BF9D436B1 (id), add constraint FK8FFE823BF9D436B1 foreign key (id) references people (id) alter table teacher add index FKAA31CBE2F9D436B1 (id), add constraint FKAA31CBE2F9D436B1 foreign key (id) references people (id)
Comparison and Selection of three ing modes-advantages and disadvantages of the three modes
For convenience, three methods are marked as [1] [2] [3] in order.
[1]: Advantages: Clear Data Structure
Disadvantage: The primary keys of the two sub-tables cannot be repeated and cannot be generated using the auto-increment mode of the database.
[2]: Advantages: High query efficiency, in line with the coarse-grained Database Design (recommended)
Disadvantage: redundant fields exist. Some fields are non-existent attributes of sub-classes.
[3]: Advantages: clear data structure, no redundancy
Disadvantage: if there are many inheritance layers of classes, more tables will be generated, and the efficiency of adding, deleting, modifying, and querying will be low.
Summary:
1. By summarizing the advantages and disadvantages of the three methods, we can find that the method of generating a table using the inheritance tree is more in line with the principle of coarse-grained database design. Of course, if the data volume is very large, you can also consider how each class generates a table.
2. The object model of the program has not changed, but the relational model has changed.
This is the benefit of hibernate. To change the relational model, you only need to change the ing file.