The inheritance mapping of 10-hibernate advanced Mapping

Source: Internet
Author: User
Tags generator object model one table

1. Object Model (Java class structure)

2. A class inheritance system a table (subclass) (table structure)


A class inheritance system a table (subclass) (mapping file)

<class name= "Employee" table= "employee" discriminator-value= "0" >

<id name= "id" >

<generator class= "native"/>

</id>

A <!-discriminator that identifies which type of employee is specific; It should be noted that the discriminator position is fixed and cannot be placed randomly, and can only be placed in front of the other attributes after the ID-->

<discriminator column= "type" type= "int"/>

<property name= "Name"/>

<subclass name= "Skiller" discriminator-value= "1" >

<property name= "Skill"/>

</subclass>

<subclass name= "Sales" discriminator-value= "2" >

<property name= "sell"/>

</subclass>

</class>

3. One table per subclass (Joined-subclass) (table structure)


one table per subclass (Joined-subclass) (mapping file)

<class name= "Employee" table= "Employee" >

<id name= "id" >

<generator class= "native"/>

</id>

<property name= "Name"/>

<joined-subclass name= "Skiller" table= "Skiller" >

<key column= "employee_id"/>

<property name= "Skill"/>

</joined-subclass>

<joined-subclass name= "Sales" table= "Sales" >

<key column= "employee_id"/>

<property name= "sell"/>

</joined-subclass>

</class>

4. Mixed use of "one class inheritance System one table" and "one table per subclass" (table structure)

mixed with "one class inheritance System one table" and "one table per subclass" (mapping file)

<class name= "Employee" table= "Employee" >

<id name= "id" >

<generator class= "native"/>

</id>

<discriminator column= "type"/>

<property name= "Name"/>

<subclass name= "Skiller" >

<property name= "NET"/>

</subclass>

<subclass name= "Sales" >

<join table= "Sales" >

<key column= "employee_id"/>

<property name= "sell"/>

</join>

</subclass>

</class>

5. One table per specific class (Union-subclass) (table structure)

one table per concrete class (Union-subclass) (mapping file)

<class name= "Employee" abstract= "true" >

<id name= "id" >

<generator class= "Hilo"/>

</id>

<property name= "Name"/>

<union-subclass name= "Skiller" table= "Skiller" >

<property name= "Skill"/>

</union-subclass>

<union-subclass name= "Sales" table= "Sales" >

<property name= "sell"/>

</union-subclass>

</class>

The primary key cannot be the identity type, and if the parent class is abstract= "true" there is no table corresponding to it.

Implicit polymorphism, the mapping file is not contacted, restrictions are more rarely used.


6. The advantages and disadvantages of the above situation

Inheritance Relationship:

1. The entire inheritance relationship is a table: the subclass has fewer properties . When you want to use a polymorphic query. Advantages: Only one table, Find Fast, query efficiency is higher; the use of polymorphic concept, in line with Java core technology. Disadvantage: If you increase the subclass table structure will be modified, the operation of trouble, if the subclass is particularly large, different subclasses of different fields are also very large, then this table will appear a lot of null value NULL.

2. One table per subclass: When there are more properties for subclasses . Advantage: There is no useless field in the database, that is, there is no null value.

3. A table for each concrete class: When the parent class is an abstract class. When a polymorphic query is not required. Disadvantages: Find the time to merge all the tables and then search, relatively slow, inefficient.

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.