An analysis of Hibernate mapping (three)--inheritance mapping

Source: Internet
Author: User

Example Object Model:

There are three ways to implement inheritance mappings:

(i) one table for each class of tree inheritance

Relational Model:

Map file:

[HTML]View Plaincopyprint?
  1. <hibernate-mapping package="Com.jialin.hibernate">
  2. <class name="Animal" table="T_animal" lazy="false">
  3. <ID name="id">
  4. <generator class="native"/>
  5. </ID>
  6. <discriminator column="type" type="string"/>
  7. <property name="name"/>
  8. <property name="Sex"/>
  9. <subclass name="Pig" discriminator-value="P">
  10. <property name="weight"/>
  11. </Subclass>
  12. <subclass name="Bird" discriminator-value="B">
  13. <property name="height"/>
  14. </Subclass>
  15. </class>
  16. </hibernate-mapping>

Description

Because the class inheritance tree must correspond to multiple classes, to store information for multiple classes in a single table, there must be some mechanism to differentiate which records belong to which class. The mechanism is to add a field to the table and differentiate it with the value of the field.

To implement this strategy with hibernate, there are the following steps:
1, the parent class with ordinary <class> tag definition

2. Define a discriminator in the parent class that specifies the name and type of the distinguished field
such as: <discriminator column= "XXX" type= "string"/>


3, subclass use <subclass> label definition, in the definition of subclass, you need to pay attention to the following points:

The Name property of the *subclass tag is the full path name of the subclass
* In the subclass tag, use the Discriminator-value attribute to indicate the value of the Discriminator field (the field used to distinguish different classes) of the class.

*subclass can be either contained in a class tag (which is exactly the inheritance relationship between classes) or parallel to the class tag. When the definition of the subclass tag is parallel to the class tag, you need to add the extends attribute in the subclass tag, which is the full path name of the parent class.
* Other properties of subclasses, like normal classes, are defined inside the subclass tag.

* For the identification value, hibernate is automatically stored when it is stored, and the relevant object is obtained according to the identification value when loading.

(ii) one table per class

Relational Model:

Map file:

[HTML]View Plaincopyprint?
  1. <hibernate-mapping package="Com.jialin.hibernate">
  2. <class name="Animal" table="T_animal">
  3. <ID name="id">
  4. <generator class="native"/>
  5. </ID>
  6. <property name="name"/>
  7. <property name="Sex"/>
  8. <joined-subclass name="Pig" table="T_pig">
  9. <key column="pid"/>
  10. <property name="weight"/>
  11. </joined-subclass>
  12. <joined-subclass name="Bird" table="T_bird">
  13. <key column="bid"/>
  14. <property name="height"/>
  15. </joined-subclass>
  16. </class>
  17. </hibernate-mapping>

Description

This strategy uses the Joined-subclass tag to define the subclass. A parent class, a subclass, and each class corresponds to a single database table.
In the database table corresponding to the parent class, all records are actually stored, including the parent class and the child class, and in the corresponding database table of the subclass, the table defines only the fields of the attribute mappings that are specific to the subclass. The child class is associated with the parent class by the same primary key value.

When implementing this strategy, there are the following steps:
1, the parent class with ordinary <class> tag definition can
2. The parent class no longer needs to define the discriminator field
3, sub-class with <joined-subclass> label definition, in the definition of joined-subclass, you need to pay attention to the following points:
The Name property of the *joined-subclass tag is the full path name of the subclass
The *joined-subclass tag needs to contain a key tag that specifies which field is associated with the child class and the parent class. such as: <key column= "parent_key_id"/> the column here is actually the name of the mapped field that corresponds to the primary key of the parent class.
The *joined-subclass tag, which can be contained in a class tag (which is exactly the inheritance relationship between classes),
It can also be parallel to the class tag. When the definition of the joined-subclass tag is parallel to the class label,
You need to add the extends attribute in the Joined-subclass tag, where the value is the full path name of the parent class.
Other properties of subclasses, like normal classes, are defined inside the joined-subclass tag.

(c) A table for each sub-category

Relational Model:

Map file:

[HTML]View Plaincopyprint?
  1. <hibernate-mapping package="Com.jialin.hibernate">
  2. <class name="Animal" table="T_animal" abstract="true">
  3. <ID name="id">
  4. <generator class="Assigned"/>
  5. </ID>
  6. <property name="name"/>
  7. <property name="Sex"/>
  8. <union-subclass name="Pig" table="T_pig">
  9. <property name="weight"/>
  10. </union-subclass>
  11. <union-subclass name="Bird" table="T_bird">
  12. <property name="height"/>
  13. </union-subclass>
  14. </class>
  15. </hibernate-mapping>


Description

This strategy uses the Union-subclass tag to define the subclass. Each sub-class corresponds to a table, and the information for this table is complete,
That is, the field that contains all of the attribute mappings inherited from the parent class (this is what it differs from joined-subclass, the table of Joined-subclass-defined subclasses, which contains only the fields of the subclass-specific attribute mappings).

When implementing this strategy, there are the following steps:
1, the parent class with ordinary <class> label definition can
2, sub-class with <union-subclass> label definition, in the definition of union-subclass, you need to pay attention to the following points:
*union-subclass tags no longer need to contain key tags (unlike joined-subclass)
The *union-subclass tag, which can be contained in a class tag (which is exactly the inheritance relationship between classes),
It can also be parallel to the class tag. When the definition of the union-subclass tag is parallel to the class label, it needs to be in the union-subclass tag,
Add the Extends property, where the value is the full path name of the parent class.
* Other properties of subclasses, like normal classes, are defined inside the union-subclass tag. This time, though in Union-subclass
It defines only the properties of the subclass, but because it inherits the parent class, there is no need to define other properties, when mapping to a database table,
A mapped field that still contains all the properties of the parent class.

Note: The ID cannot be duplicated when saving the object (cannot generate primary key using the self-increment of the database)

An analysis of Hibernate mapping (three)--inheritance mapping

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.