JPA ing policy for Object Inheritance

Source: Internet
Author: User

Note: The entity mentioned here refers to the @ entity annotation class.

The inheritance ing is annotated using @ inheritance. The value of its strategy attribute is defined by the enumeration inheritancetype (including single_table, table_per_class, and joined, which correspond to three inheritance policies respectively ). The @ inheritance annotation can only apply to the superclasses of the inherited structure. If no inheritance policy is specified, single_table is used by default.


JPA provides three inheritance ing policies:
1. A Class inherits a table policy. This is the Default policy for inheriting mappings. That is, if entity B inherits entity Class A, and entity Class C also inherits entity a, it will only be mapped into one table, this table contains all fields in Entity classes A, B, and C. JPA uses a column called discriminator to distinguish the entity that a row of data should be mapped. Annotation: @ inheritance (Strategy = inheritancetype. single_table)
2. Combine subclass policies. In this case, the subclass fields are mapped to their respective tables. These fields include the fields in the parent class, And a join operation is executed to instantiate the subclass. Annotation: @ inheritance (Strategy = inheritancetype. Joined)
3. One table policy for each specific class. Annotation: @ inheritance (Strategy = inheritancetype. table_per_class)

I. A Class inherits the structure of a table.
In this policy, all classes in an inheritance structure are mapped to a table. This table uses a column as the "discriminator column" to identify a row of data belonging to a specified subclass instance.
This ing policy provides good support for the entity and the multi-state system that queries the class inheritance structure. However, the disadvantage is that the column corresponding to the specified state of the subclass can be empty.

Example:

Package COM. mikan; import Java. io. serializable; import javax. persistence. column; import javax. persistence. discriminatorcolumn; import javax. persistence. entity; import javax. persistence. generatedvalue; import javax. persistence. generationtype; import javax. persistence. ID; import javax. persistence. inheritance; import javax. persistence. inheritancetype; import javax. persistence. table; @ entity @ table (name = "EMP") @ inheritance (Strategy = inheritancetype. single_table) @ discriminatorcolumn (name = "emp_type") public class employee implements serializable {Private Static final long serialversionuid =-ments; @ ID @ generatedvalue (Strategy = generationtype. identity) protected integer empid; @ columnprotected string name; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. discriminatorvalue; import javax. persistence. entity; @ entity @ discriminatorvalue ("ft") public class fulltimeemployee extends employee {Private Static final long serialversionuid = region; @ columnprivate double salary; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. discriminatorvalue; import javax. persistence. entity; @ entity @ discriminatorvalue ("PT") public class parttimeemployee extends employee {Private Static final long serialversionuid =-regular; @ column (name = "hourly_wage") Private float hourlywage; // getter/setter method}
The @ discriminatorcolumn annotation of the superclass can be omitted. The default "discriminator column" is dtype, and the default type is string.
@ Discriminatorcolumn annotation can only be used on superclasses and cannot be used on specific subclasses. The value of discriminatortype is defined by the discriminatortype enumeration, including string, Char, and integer. If discriminatortype is specified, the value of @ discriminatorvalue Annotation on the subclass should also be of the corresponding type.
@ Discriminatorvalue annotation can only be used on a specific object subclass. Similarly, the @ discriminatorvalue annotation can be omitted. The class name is used as the value by default.
In the preceding example, only one table is generated, including the emp_type, empid, name, salary, and hourly_wage fields. When fulltimeemployee is saved, the value of emp_type is "ft". When parttimeemployee is saved, the value of emp_type is "PT ".

Ii. Joint subclass Policy
This type of policy superclass will be mapped into a separate table, and each subclass will also be mapped into a separate table. The table corresponding to the subclass only contains fields corresponding to its own attributes. By default, the primary key is used as the foreign key of the table corresponding to the superclass.
This policy provides good support for the polymorphism between entities. However, when instantiating a subclass instance, one or more tables need to be joined. In a deep inheritance structure, this causes low performance. Example:
Package COM. mikan; import Java. io. serializable; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. generatedvalue; import javax. persistence. generationtype; import javax. persistence. ID; import javax. persistence. inheritance; import javax. persistence. inheritancetype; import javax. persistence. table; @ entity @ table (name = "EMP") @ inheritance (Strategy = inheritancetype. joined) public class employee implements serializable {Private Static final long serialversionuid =-7674179980281525370l; @ ID @ generatedvalue (Strategy = generationtype. identity) protected integer empid; @ columnprotected string name; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. discriminatorvalue; import javax. persistence. entity; import javax. persistence. table; @ entity @ table (name = "ft_emp") public class fulltimeemployee extends employee {Private Static final long serialversionuid = 9115429216382631425l; @ columnprivate double salary; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. discriminatorvalue; import javax. persistence. entity; import javax. persistence. table; @ entity @ table (name = "pt_emp") public class parttimeemployee extends employee {Private Static final long serialversionuid =-61221_3745158301_l; @ column (name = "hourly_wage ") private float hourlywage; // getter/setter method}
This will be mapped to three specific tables, namely, the EMP table corresponding to the employee. The fields include empid and name; the ft_emp table corresponding to fulltimeemployee; the fields include empid and salary; and The pt_emp table corresponding to parttimeemployee, fields include empid and hourly_wage. The empid in the ft_emp and pt_emp tables serves as the foreign key of the table EMP, which is also the primary key. By default, the primary key of the superclass is used as the primary key and foreign key of the subclass. Of course, you can use the @ primarykeyjoincolumn annotation to specify the name of the foreign key. For example, if fulltimeemployee uses the @ primarykeyjoincolumn (name = "ft_empid") annotation, the fields of this subclass object are ft_empid and name, ft_empid is used as the primary key of the ft_time table. It is also the foreign key of the EMP table.
Each time a subclass object stores a piece of data, a record is inserted in the EMP table. For example, if a data entry is inserted in the ft_emp table, the name is inserted in the EMP table and the empid is generated, insert empid and salary in the ft_emp table. The same applies to pt_emp.
A table is generated no matter whether the superclass is an abstract class or a specific class.

3. One table policy for each specific class
In this ing policy, each class is mapped to a separate table. All attributes of the class, including inherited attributes, are mapped to the columns of the table.
The disadvantage of this ing policy is that the support for polymorphism is limited. When a query involves a class inheritance structure, an SQL Union query is usually required. Example:
Package COM. mikan; import Java. io. serializable; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. generatedvalue; import javax. persistence. generationtype; import javax. persistence. ID; import javax. persistence. inheritance; import javax. persistence. inheritancetype; import javax. persistence. table; @ entity @ table (name = "EMP") @ inheritance (Strategy = inheritancetype. table_per_class) public class employee implements serializable {Private Static final long serialversionuid =-7674179980281525370l; @ ID @ generatedvalue (Strategy = generationtype. table) protected integer empid; @ columnprotected string name; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. discriminatorvalue; import javax. persistence. entity; import javax. persistence. table; @ entity @ table (name = "ft_emp") public class fulltimeemployee extends employee {Private Static final long serialversionuid = 9115429216382631425l; @ columnprivate double salary; // getter/setter method} package COM. mikan; import javax. persistence. column; import javax. persistence. discriminatorvalue; import javax. persistence. entity; import javax. persistence. table; @ entity @ table (name = "pt_emp") public class parttimeemployee extends employee {Private Static final long serialversionuid =-61221_3745158301_l; @ column (name = "hourly_wage ") private float hourlywage; // getter/setter method}
This will be mapped to three specific tables, namely, the EMP table corresponding to the employee. The fields include empid and name; the ft_emp table corresponding to fulltimeemployee; the fields include empid and salary; and The pt_emp table corresponding to parttimeemployee, fields include empid and hourly_wage. The empid in the ft_emp and pt_emp tables has no relationship with the empid In the EMP table. Every time a subclass object stores a piece of data, records are not inserted in the EMP table.
In addition, the generationtype. Auto or generationtype. identity cannot be used for the primary key generation policy. Otherwise, an exception occurs:
Org. hibernate. mappingexception: cannot use identity column key generation with <Union-subclass> mapping for: COM. Mikan. parttimeemployee

Because the table_per_class policy is separate for each table and does not have any relationship with the primary keys of each table, you cannot use generationtype. Auto or generationtype. Identity to generate a primary key. You can use generationtype. Table.

Specific reference: http://stackoverflow.com/questions/916169/cannot-use-identity-column-key-generation-with-union-subclass-table-per-clas

If the superclass is an abstract class, no corresponding table is generated. If the superclass is a specific class, the corresponding table is generated.

The above example passes the test using JPA hibernate.

JPA ing policy for Object Inheritance

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.