Hibernate object ing ---- inheritance ing

Source: Internet
Author: User

Single-Table inheritance ing
Each class inheritance tree uses a table.



The extening file Extends. hbm. xml.
[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Animal" table = "t_animal" lazy = "false">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Discriminator column = "type" type = "string"/>
<Property name = "name"/>
<Property name = "sex"/>
<Subclass name = "Pig" discriminator-value = "P">
<Property name = "weight"/>
</Subclass>
<Subclass name = "Brid" discriminator-value = "B">
<Property name = "height"/>
</Subclass>
</Class>
</Hibernate-mapping>
Because the class inheritance tree must correspond to multiple classes, to store the information of multiple classes in a table, there must be a mechanism to distinguish which records belong to which classes. This mechanism adds a field to the table and uses the value of this field for differentiation. To implement this policy using hibernate, follow these steps:
(1) The parent class is defined by a common <class> label.
(2) define a discriminator in the parent class, that is, specify the name and type of the distinguished Field
For example: <discriminator column = "XXX" type = "string"/>
(3) Use the <subclass> label to define sub-classes. When defining subclass, pay attention to the following points:
1. the name attribute of the Subclass label is the full path name of the Subclass;
2. In the Subclass tag, use the discriminator-value attribute to indicate the value of the discriminator field (used to distinguish fields of different classes) of the Subclass;
3. The Subclass tag can be included by the class tag (this inclusion relationship indicates the inheritance relationship between classes) or parallel to the class tag. When the subclass label definition is parallel to the class label, you must add the extends attribute to the subclass label. The value in the attribute is the full path name of the parent class.
4. other attributes of sub-classes, like common classes, are defined within the subclass tag.
Specific table inheritance ing
Each specific class is a table. (Same as above)

The extening file Extends. hbm. xml.
[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Animal" table = "t_animal" lazy = "false" abstract = "true">
<Id name = "id">
<Generator class = "assigned"/>
</Id>
<Property name = "name"/>
<Property name = "sex"/>
<Union-subclass name = "Pig" table = "t_pig">
<Property name = "weight"/>
</Union-subclass>
<Union-subclass name = "Brid" table = "t_brid">
<Property name = "height"/>
</Union-subclass>
</Class>
</Hibernate-mapping>
This policy uses the union-subclass label to define sub-classes. Each subclass corresponds to a table, and the information of this table is complete, that is, it contains all the attribute ing fields inherited from the parent class (this is the difference between it and joined-subclass, table of child classes defined by joined-subclass, which only contains fields mapped to the properties of child classes ). To implement this policy, follow these steps:
(1) The parent class can be defined by a common <class> label;
(2) subclass is defined using the <union-subclass> label. When defining union-subclass, pay attention to the following points:
1. The Union-subclass tag no longer needs to contain the key tag (different from joined-subclass ).
2. The Union-subclass label can be included by the class label (this inclusion relationship indicates the inheritance relationship between classes) or parallel to the class label. When the definition of the Union-subclass label is parallel to the class label, you must add the extends attribute to the Union-subclass label. The value in the attribute is the full path name of the parent class.
3. Other attributes of sub-classes, like common classes, are defined within the Union-subclass label. At this time, although union-subclass defines only the attributes of sub-classes, it inherits the parent class, so no other attributes need to be defined, when ing to a database table, it still contains the ing fields of all attributes of the parent class.
Note: the id of the stored object cannot be repeated (the primary key cannot be generated using the auto-increment mode of the database)
Class table inheritance ing
Each class has one table. (Same as above)

 
The extening file Extends. hbm. xml.
[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Animal" table = "t_animal" lazy = "false">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Property name = "sex"/>
<Joined-subclass name = "Pig" table = "t_pig">
<Key column = "pid"/>
<Property name = "weight"/>
</Joined-subclass>
<Joined-subclass name = "Brid" table = "t_brid">
<Key column = "bid"/>
<Property name = "height"/>
</Joined-subclass>
</Class>
</Hibernate-mapping>
This policy uses the joined-subclass tag to define sub-classes. Parent class and subclass. Each class corresponds to a database table. In the database table corresponding to the parent class, all records, including records of the parent class and subclass, are stored. In the database table corresponding to the Child class, this table only defines the attribute ing fields specific to the subclass. Child classes and parent classes are associated by the same primary key value. To implement this policy, follow these steps:
(1) The parent class can be defined using a common <class> label. The parent class no longer needs to define the discriminator field;
(2) The subclass is defined using the <joined-subclass> label. Note the following when defining joined-subclass:
1. the name attribute of the Joined-subclass tag is the full path name of the subclass.
2. the Joined-subclass tag must contain a key tag, which specifies the field used to associate the subclass with the parent class.
For example, <key column = "PARENT_KEY_ID"/>. The column here is actually the name of the ing field corresponding to the primary key of the parent class.
3. the Joined-subclass tag can be included by the class tag (this inclusion relationship indicates the inheritance relationship between classes) or parallel to the class tag. When the Joined-subclass label is defined in parallel with the class label, you must add the extends attribute to the Joined-subclass label. The value in the attribute is the full path name of the parent class.
4. other attributes of the subclass, like common classes, are defined within the joined-subclass tag.
Author: StubbornPotatoes

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.