Mappings of JPA Object Inheritance relationships

Source: Internet
Author: User

In JPA, there are three ing policies for Object Inheritance relationships: Table per class, table per subclass, and table_per_class.

 

1. Single Table inheritance Policy

Single-Table inheritance policy. Parent-class entities and sub-class entities share a database table. In the table, a column is used to identify fields to differentiate entities of different classes. The procedure is as follows:

A. Add the following annotation under the @ entity annotation of the parent class object:

@ Inheritance (Strategy = inheritancetype. single_table)
@ Discriminatorcolumn (name = "identifying field column names ")
@ Discriminatorvalue (column value of the parent entity identification field)

 

B. Add the following annotation under the @ entity annotation of the subclass object:

@ Discriminatorvalue (column value of the field to be recognized by the subclass object)

 

 

Defines a parent class

 

Java code
  1. @ Entity
  2. @ Inheritance (Strategy = inheritancetype. single_table)
  3. @ Table (name = "window_file ")
  4. @ Discriminatorcolumn (name = "discriminator", discriminatortype = discriminatortype. String, length = 30)
  5. @ Discriminatorvalue ("windowfile ")
  6. Public class windowfile {
  7. @ ID
  8. @ Generatedvalue (Strategy = generationtype. Auto)
  9. Private integer ID;
  10. @ Basic
  11. @ Column (name = "name ")
  12. Private string name;
  13. @ Basic
  14. @ Column (name = "type ")
  15. Private string type;
  16. @ Basic
  17. @ Column (name = "date ")
  18. Private date;
  19. // Omit Get Set
  20. }

 

And then define two subclasses.

 

Java code
  1. @ Entity
  2. @ Discriminatorvalue ("folder ")
  3. Public class folder extends windowfile {
  4. @ Basic
  5. @ Column (name = "file_count ")
  6. Private integer filecount;
  7. // Omit Get Set
  8. }

 

Java code
  1. @ Entity
  2. @ Discriminatorvalue ("document ")
  3. Public class document extends windowfile {
  4. @ Basic
  5. @ Column (name = "size ")
  6. Private string size;
  7. // Omit Get Set
  8. }

 

Different discriminator columns are used to differentiate Parent and Child entities.

 

The actual table structure is as follows:

Window_file discriminator, ID, name, date, type, size, file_count

 

When you use a windowfile object, the actual table field is discriminator = 'windowfile', and the size and file_count are always empty.

When the folder object is used, discriminator = 'folder', size is always empty, and file_count is the actual value.

Similarly, document is similar to folder.

 

2. Joined Policy

 

The parent object and the child object correspond to different tables in the database respectively. The child object table only has its extended special attributes, and the public attributes of the parent class are stored in the parent class Object ing table. Specific Practices:

 

 

 

@ Inheritance (Strategy = inheritancetype. Joined)

 

Special descriptions are not required for subclass objects.

 

 

Java code
  1. @ Entity
  2. @ Table (name = "t_animal ")
  3. @ Inheritance (Strategy = inheritancetype. Joined)
  4. Public class animal {
  5. @ ID
  6. @ Column (name = "ID ")
  7. @ Generatedvalue (Strategy = generationtype. Auto)
  8. Private integer ID;
  9. @ Column (name = "name ")
  10. Private string name;
  11. @ Column (name = "color ")
  12. Private string color;
  13. // Omit Get Set
  14. }

 

Java code
  1. @ Entity
  2. @ Table (name = "t_bird ")
  3. @ Primarykeyjoincolumn (name = "bird_id ")
  4. Public class bird extends animal {
  5. @ Column (name = "Speed ")
  6. Private string speed;
  7. // Omit Get Set
  8. }

 

Java code
  1. @ Entity
  2. @ Table (name = "t_dog ")
  3. @ Primarykeyjoincolumn (name = "dog_id ")
  4. Public class dog extends animal {
  5. @ Column (name = "Legs ")
  6. Private integer legs;
  7. // Omit Get Set
  8. }

 

 

The actual table structure is as follows:

T_animal ID, color, name

T_bird speed, bird (both a foreign key and a primary key)

T_dog legs, dog_id (both foreign key and primary key)

 

3. table_per_class policy:

 

Table_per_class policy. each class of the parent class Object corresponds to a table in the database, and all attributes, including attributes inherited from the parent class object, are saved in the Child class table. Specific Practices:

You only need to add the following annotation under the @ entity annotation of the parent class object:

@ Inheritance (Strategy = inheritancetype. table_per_class)

 

Java code
  1. @ Entity
  2. @ Inheritance (Strategy = inheritancetype. table_per_class)
  3. @ Table (name = "t_vehicle ")
  4. Public class vehicle {// base class
  5. @ ID
  6. // @ Generatedvalue
  7. @ Column (name = "ID ")
  8. Private integer ID;
  9. @ Column (name = "Speed ")
  10. Private integer speed; // speed
  11. // Omit Get Set
  12. }

 

Java code
  1. @ Entity
  2. @ Table (name = "t_car ")
  3. Public class car extends vehicle {
  4. @ Column (name = "engine ")
  5. Private string engine; // Engine
  6. // Omit Get Set
  7. }

 

Once this policy is used, auto generator and identity generator cannot be used, that is, the primary key value cannot be automatically generated by the database.

 

The actual table structure is as follows:

T_vehicle ID, speed

T_car ID, speed, engine



Address: http://www.360doc.com/content/13/0710/15/203871_298949089.shtml

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.