Previous relationships we used to use, for inheritance we are not unfamiliar, often contact between the class and the inheritance of the class with the extends keyword, then in the table and the relationship between the table how to express it? Let's follow the inheritance map.
Inheritance has three implementation strategies, single table inheritance, concrete table inheritance, Class table inheritance. Here's a look at the three ways of doing this.
Inheriting association class relationships
single table inheritance
Each class inherits the tree using a table, which shows that these three classes are in a table. As the following table:
This table includes all the attributes of the parent class, the subclass, and the type to distinguish which subclass.
The object model maps to the relational model:
<classname= "Com.bjpowernode.hibernat.Animal" table= "T_animal" ><idname= "id" ><generatorclass= " Native "/></id><!--identification field, defined in the parent class, specifying the distinguished field name and type--><discriminatorcolumn=" type "type=" string "/> <propertyname= "name"/><propertyname= "sex"/><!--identification value--><subclassname= "Pig" Discriminator-value= "P" ><propertyname= "Weight"/></subclass><subclassname= "Bird" Discriminator-value= "B" ><propertyname= "height"/></subclass></class>
Each class inherits the tree one table, the class inheritance tree corresponds to more than one class, and to put more than one class of information into a table, there must be some mechanism to distinguish which records belong to which class. that is, the above discriminator field, which is distinguished by the value of this field.
Advantages and Disadvantages: The table introduces the fields used for the zone molecule class, if the property of a subclass cannot be empty, the field cannot be set in the database is not empty, the flexibility is poor, maintenance is convenient, only one table needs to be modified, and if the subclass increases, the redundant fields in the table will increase. If the data is not many, efficiency is the best.
Concrete Table Inheritance
Each subclass is a table that is associated with the table of the parent class in the same way as the primary key, as follows
Animal all records of a subclass are stored in the table, and only public information is logged. Their unique information is stored in the subclass table. The parent table is associated with the child table's ID .
Object model mapping to relational model
<classname= "Com.bjpowernode.hibernat.Animal" table= "T_animal" ><idname= "id" ><generatorclass= " Native "/></id> <propertyname=" name "/><propertyname=" Sex "/> <joined-subclassname=" Pig " Table= "T_pig" ><keycolumn= "pid"/><propertyname= "Weight"/></joined-subclass> < Joined-subclassname= "Bird" table= "T_bird" ><keycolumn= "Bird"/><propertyname= "height"/></ Joined-subclass></class>
This class inherits mappings with the <joined-subclass> label , The associated field that indicates the parent class and child class.
Advantages and Disadvantages: This approach conforms to the design principle of the relational model, there is no redundancy, it is easier to maintain, the modification of each class only needs to modify its corresponding table, flexibility, and fully refer to the way the object inherits. Another level is clear, but if the class inherits a lot of layers, the table is particularly long, the efficiency will be very low, so if the level of less can be used in this way.
Class Table Inheritance
each concrete Class A table, according to the above class diagram, a total of two tables, Pig , Bird each one
The corresponding database table for each subclass includes not only its own properties but also the properties of the parent class.
Object model mapping to relational model
<classname= "Com.bjpowernode.hibernat.Animal" table= "T_animal" ><idname= "id" ><generatorclass= " Native "/></id> <propertyname=" name "/><propertyname=" Sex "/> <union-subclassname=" Pig " Table= "T_pig" ><keycolumn= "pid"/><propertyname= "Weight"/></union-subclass> < Union-subclassname= "Bird" table= "T_bird" ><keycolumn= "Bird"/><propertyname= "height"/></ Union-subclass></class>
This type of inheritance mapping is used <union-subclass> label that indicates the HBM Subclass of the class represented by the file
Pros and Cons: This approach conforms to the design principles of the relational model, but there are duplicate fields in the table, and if you modify the Public Properties section, you need to modify the attribute values in the table for all the subclasses, and the mapping flexibility is great. In addition, queries for subclasses require only access to separate tables, and queries for the parent class need to retrieve all the tables.
Summary
Summarize the advantages and disadvantages of the above three ways:
from the point of view of complexity, the way 1 is simple (sub-class attribute is not many); mode 2 primary foreign key; mode 3 has repeating field;
&NBSP, From query performance, way 1 high efficiency; mode 2 requires in-table or left outer joins; way 3
, "en-US" lang= " Font-family:calibri ">1 2 . Span lang= "en-US" style= "Font-family:calibri" >3 If the Parent property changes, you need to modify the table for all child classes.
that is, when the sub-class attribute is not long, preferred mode 1,sub-class attributes, the requirements are not strict, preferred mode 2.
after reading, the inheritance map is not so esoteric. Three ways to sound very powerful, in fact, we have learned about the main foreign key, the third table, a variety of connections. These are the foundations.