[Hibernate learning]-ORM (III)

Source: Internet
Author: User

We used to use the previous relationships. We are not unfamiliar with inheritance. The extends keyword is used for inheritance between classes that we often come into contact, how can we represent the relationship between a table and a table? Next we will discuss the inheritance ing.

Inheritance has three implementation policies: Single-Table inheritance, specific table inheritance, and class-Table inheritance. The following three methods are analyzed:

 

Inherit association relationships


Single Table inheritance

Each class inheritance tree uses one table. We can see that these three classes are in one table. See the following table:



This table includes all attributes of the parent class and subclass. The Type is used to identify which subclass is used.

 

Object Model ing to relational model:

<Classname = "com. bjpowernode. hibernat. Animal" table = "t_animal"> <idname = "id"> <generatorclass = "native"/> </id> <! -- Identifies a field, defined in the parent class, specify the name and type of the distinguished fields --> <discriminatorcolumn = "type" type = "string"/> <propertyname = "name"/> <propertyname = "sex"/> <! -- Authentication value --> <subclassname = "Pig" discriminator-value = "P"> <propertyname = "weight"/> </subclass> <subclassname = "Bird" discriminator-value = "B"> <propertyname = "height"/> </subclass> </class>

 

Each class inheritance tree is a table. The class inheritance tree corresponds to multiple classes. You need to put the information of multiple classes in one table, there must be a mechanism to distinguish which records belong to which class. That is, the above discriminator field is distinguished by the value of this field.

 

Advantages and disadvantages: fields used to differentiate sub-classes are introduced in the table. If the attribute of a sub-class cannot be empty, this field cannot be set in the database to be non-empty and has poor flexibility. It is easy to maintain, you only need to modify one table. If the child class is added, the redundant fields in the table will also increase. If there is not much data, the efficiency is the best.

 

Specific table inheritance

A table of each subclass is associated with the table corresponding to the parent class in a one-to-one primary key Association mode.


The animal table stores all records of sub-classes and only records public information. Their unique information is stored in the subclass table. Associate the parent table with the child table id.

 

Object Model ing 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>

The <joined-subclass> label is used to indicate the fields associated with the parent class and subclass.

 

Advantages and disadvantages: This method conforms to the design principles of the relational model and does not have redundancy. It is easy to maintain. To modify each class, you only need to modify the corresponding table, which is flexible; configure the object by referring to the Object Inheritance Method. In addition, the hierarchy is clear, but the efficiency is very low if there are many layers of class inheritance and many tables, so this method can be used if there are few layers.

 

Class table inheritance

Each specific class is a table. According to the class diagram above, there are two tables, one for pig and one for bird.


 

The database table corresponding to each subclass includes not only its own attributes but also the attributes of the parent class.

Object Model ing 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>

The <union-subclass> label is used to indicate the subclass of the class represented by the hbm file.

Advantages and disadvantages: This method conforms to the design principles of the relational model, but there are repeated fields in the table. If you modify the public attribute, You need to modify the attribute values of the tables corresponding to all subclasses, ing is flexible. In addition, for subclass queries, you only need to access individual tables. For parent queries, You need to retrieve all tables.

 

Summary

Summarize the advantages and disadvantages of the above three methods:

From the complexity perspective, method 1 is simple (not many subclass attributes); method 2 is the primary foreign key; Method 3 has repeated fields;

From query performance, method 1 is highly efficient; method 2 requires intra-Table connections or left outer connections; Method 3: if the parent class needs to be queried, all class tables must be queried;

From maintainability, method 1 only needs to modify one table; Method 2: Modify the corresponding table if a property changes; Method 3: Modify the table corresponding to all child classes if the parent class property changes.

That is to say, if there are not many subclass attributes, select method 1 first. If there are many subclass attributes and the requirements are not strict, select method 2 first.

After reading this, the inheritance ing is not that esoteric. The three methods sound amazing. They actually focus on the primary and Foreign keys we have learned before, the third table, and various connections. These are the basis.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.