Object Model example:
There are three methods to implement the inheritance ing:
(1) Each class inherits one table from the tree
LINK model:
Ing file:
[Html]
<Hibernate-mapping package = "com. jialin. 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 = "Bird" discriminator-value = "B">
<Property name = "height"/>
</Subclass>
</Class>
</Hibernate-mapping>
<Hibernate-mapping package = "com. jialin. 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 = "Bird" discriminator-value = "B">
<Property name = "height"/>
</Subclass>
</Class>
</Hibernate-mapping>
Note:
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 to 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. Note the following when defining subclass:
* The name attribute of the Subclass label 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 (used to distinguish fields of different classes) of the Subclass.
* Subclass can be included by class labels (this inclusion relationship indicates the inheritance relationship between classes), or it can be parallel to class labels. 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.
* Other attributes of sub-classes are defined within the subclass label like ordinary classes.
* Hibernate automatically stores the authentication value when it is stored. During loading, it obtains the relevant objects based on the authentication value.
(2) One table for each class
LINK model:
Ing file:
[Html]
<Hibernate-mapping package = "com. jialin. hibernate">
<Class name = "Animal" table = "t_animal">
<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 = "Bird" table = "t_bird">
<Key column = "bid"/>
<Property name = "height"/>
</Joined-subclass>
</Class>
</Hibernate-mapping>
<Hibernate-mapping package = "com. jialin. hibernate">
<Class name = "Animal" table = "t_animal">
<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 = "Bird" table = "t_bird">
<Key column = "bid"/>
<Property name = "height"/>
</Joined-subclass>
</Class>
</Hibernate-mapping>
Note:
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.
2. the discriminator field is no longer required for the parent class.
3. The subclass is defined using the <joined-subclass> label. Note the following when defining joined-subclass:
* The name attribute of the Joined-subclass tag is the full path name of the subclass.
* 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.
* The Joined-subclass tag can be included by the class tag (this inclusion relationship indicates the inheritance relationship between classes ),
It can also be parallel to the class label. When the Joined-subclass label definition is parallel to the class label,
You need to add the extends attribute to the Joined-subclass tag. The value in the attribute is the full path name of the parent class.
Other attributes of sub-classes, like common classes, are defined within the joined-subclass tag.
(3) one table for each subclass
LINK model:
Ing file:
[Html]
<Hibernate-mapping package = "com. jialin. hibernate">
<Class name = "Animal" table = "t_animal" 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 = "Bird" table = "t_bird">
<Property name = "height"/>
</Union-subclass>
</Class>
</Hibernate-mapping>
<Hibernate-mapping package = "com. jialin. hibernate">
<Class name = "Animal" table = "t_animal" 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 = "Bird" table = "t_bird">
<Property name = "height"/>
</Union-subclass>
</Class>
</Hibernate-mapping>
Note:
This policy uses the union-subclass label to define sub-classes. Each subclass corresponds to a table, and the table information is complete,
That is, it contains all the property ing fields inherited from the parent class (this is the difference between it and joined-subclass, the table of child classes defined by joined-subclass, only fields that contain the property ing of the subclass ).
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:
* The Union-subclass tag does not need to contain the key tag (unlike joined-subclass)
* The Union-subclass tag can be included by the class tag (this inclusion relationship indicates the inheritance relationship between classes ),
It can also be parallel to the class label. When the definition of the Union-subclass label is parallel to the class label, you must use the Union-subclass label,
Add the extends attribute. The value is the full path name of the parent class.
* Other attributes of sub-classes, like common classes, are defined within the Union-subclass label. In this case
Only subclass attributes are defined in it, but because it inherits the parent class, 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)