Hibing relationship of Hibernate4.x -- Inheritance ing, hibernate4.x --
The inheritance ing of Hibernate can be understood as the inheritance relationship between persistence classes. For example, the relationship between people and students. If a student inherits a person, he or she can think of the student as a special person. If the student queries the person, the student instance will also be obtained.
Hibernate supports the following three inheritance ing policies:
The following describes the three mappings. Use Person as the parent class and Student as the subclass to explain in detail the implementation of the above three policies. Perosn (id, name, age), Studnet (school)
Use subclass for ing:
- Using the subclass inheritance ing, you can use the same table for the parent class and subclass in the inheritance relationship.
- Because the instances of the parent class and subclass are all stored in the same table, you need to add a column to the table, this column is used to identify the instance of the class in which each record is located. This column is called a discriminator)
- In this ing policy, subclass is used to map child classes and the discriminator-value Attribute of class or subclass is used to specify the values of the speaker column.
- All fields defined by subclass cannot have non-null constraints. If a non-null constraint is added for those fields, the instance of the parent class does not actually refer to those columns. This will cause a database integrity conflict and cause the instance of the parent class to be unable to be saved to the database.
Person. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-30 9:34:50 by Hibernate Tools 3.4.0.CR1 --> 5
Use joined-subclass for ing
- You can use the inheritance ing of joined-subclass elements to create a table for each subclass.
- When this ing policy is used, the parent class instance is stored in the parent class table, and the Child class instance is stored in the parent class table and the Child class table. Because the subclass instance is also a special parent class instance, it must also contain the attributes of the parent class instance. Therefore, the attributes common to the Child class and the parent class are saved in the parent class table. The attributes added to the Child class are saved in the Child class table.
- In this ing policy, you do not need to use the reporters column, but you need to use the key element for each subclass to map a common primary key.
- You can add non-null constraints to attributes added to subclass. Because the attributes of subclass and parent classes are not saved in the same table
Person. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-30 9:34:50 by Hibernate Tools 3.4.0.CR1 --> 5
Use union-subclass for ing
- The union-subclass element can be used to map each object to an independent table.
- Attributes added to subclass can have non-null constraints. That is, the data of the parent class instance is stored in the parent table, and the data of the Child class instance is stored in the Child class table.
- The data of the subclass instance is only stored in the subclass table, but there is no record in the parent table.
- In this ing policy, the fields in the subclass table are more than those in the parent table, because the fields in the subclass table are equal to the fields in the parent table and add subclass to add the sum of attributes.
- In this ing policy, you do not need to use the identifier column or the key element to map a common primary key.
- The union-subclass ing policy cannot be used to generate an identity primary key. Because all entity classes in the same class hierarchy must use the same primary key seed, that is, the primary keys of records corresponding to multiple persistent entities should be continuous, native primary key generation policy should not be used, because native will choose to use identity or sequence based on the database.
Person. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-30 9:34:50 by Hibernate Tools 3.4.0.CR1 --> 5