Hibernate ing-inheritance ing

Source: Internet
Author: User

There are many animals in the real world, such as pig (pig) and bird (BIRD). When I use object-oriented thinking for analysis, we usually extract the common parts to add an abstract class animal (animal ), in this way, when writing programs, pig and bird only need to inherit their parent class animal to save a lot of repeated code. In Java code, this inheritance relationship can be easily implemented with only the extends keyword, but there is no keyword for the relational database we use to specify this inheritance relationship. To reflect this inheritance relationship to the database, Hibernate provides three solutions:

1. Each specific class corresponds to a table

This solution enables each specific class in the inheritance system to correspond to a table in the database. As follows:


The database table corresponding to each subclass contains information about the parent class and has its own unique attributes. each subclass corresponds to a table with complete information, this policy includes all attributes mapped to the parent class. The <Union-subclass> label is used to define the subclass.

Note: the ID of the stored object cannot be repeated (the primary key cannot be generated using the auto-increment mode of the database)

Some code display: 

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

2. One table for each class

This policy uses the <joined-subclass> label to define sub-classes. The parent class and Child class correspond to a database table. In the database table corresponding to the parent class, it stores the public information of all records. In fact, the table corresponding to the parent class contains all records, including records of the parent class and subclass; in the database table corresponding to the subclass, this table only defines the attribute ing fields specific to the subclass. The data table corresponding to the subclass and the data table corresponding to the parent class are joined by one-to-one primary key Association.

For this policy:


The t_animal table stores all records of sub-classes, but only records the information they share. Their unique information is stored in their corresponding table. A record needs to obtain its unique information, you need to use the primary key of the t_animal record to find the record with the same primary key value in its corresponding sub-table and then retrieve its unique information.

Note: The name attribute of the joined-subclass label 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), or it can be flat with 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.
Other attributes of sub-classes, like common classes, are defined within the joined-subclass tag.

Some code display:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

3. Each class inherits one table from the tree.

This policy is implemented using the <subclass> label. Because there are many sub-classes in the class inheritance system, to store the information of multiple classes in a table, there must be a mechanism to distinguish which records belong to which class. In hibernate, this mechanism adds a field to the table and uses the value of this field for differentiation. Add this label column in the table using the <discriminator> label.

Policy:


After all the class information in the inheritance system is displayed in the same table, null is automatically assigned as long as the class does not have any attribute.

Note: 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, it can be included by the class label (this inclusion relationship indicates the inheritance relationship between classes), or it can be parallel to the class label. 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.
Some code display:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Summary: each class inherits a tree. A table has better efficiency because it has only one table, and only one table is operated, whether Stored, searched, or updated, we all know that such a table structure has a disadvantage that there will be more redundant fields. A table of each class has a distinct hierarchy, but if the inheritance structure is deep, there will be more associations, in this way, multiple tables are associated for query, which is less efficient. A table of each specific class has some restrictions on primary keys. native-like auto-incrementing methods cannot be used. Therefore, we generally use more of the first two methods. We need to combine the advantages and disadvantages of the three methods based on the development requirements.

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.