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
- @ Entity
- @ Inheritance (Strategy = inheritancetype. single_table)
- @ Table (name = "window_file ")
- @ Discriminatorcolumn (name = "discriminator", discriminatortype = discriminatortype. String, length = 30)
- @ Discriminatorvalue ("windowfile ")
- Public class windowfile {
- @ ID
- @ Generatedvalue (Strategy = generationtype. Auto)
- Private integer ID;
- @ Basic
- @ Column (name = "name ")
- Private string name;
- @ Basic
- @ Column (name = "type ")
- Private string type;
- @ Basic
- @ Column (name = "date ")
- Private date;
- // Omit Get Set
- }
And then define two subclasses.
Java code
- @ Entity
- @ Discriminatorvalue ("folder ")
- Public class folder extends windowfile {
- @ Basic
- @ Column (name = "file_count ")
- Private integer filecount;
- // Omit Get Set
- }
Java code
- @ Entity
- @ Discriminatorvalue ("document ")
- Public class document extends windowfile {
- @ Basic
- @ Column (name = "size ")
- Private string size;
- // Omit Get Set
- }
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
- @ Entity
- @ Table (name = "t_animal ")
- @ Inheritance (Strategy = inheritancetype. Joined)
- Public class animal {
- @ ID
- @ Column (name = "ID ")
- @ Generatedvalue (Strategy = generationtype. Auto)
- Private integer ID;
- @ Column (name = "name ")
- Private string name;
- @ Column (name = "color ")
- Private string color;
- // Omit Get Set
- }
Java code
- @ Entity
- @ Table (name = "t_bird ")
- @ Primarykeyjoincolumn (name = "bird_id ")
- Public class bird extends animal {
- @ Column (name = "Speed ")
- Private string speed;
- // Omit Get Set
- }
Java code
- @ Entity
- @ Table (name = "t_dog ")
- @ Primarykeyjoincolumn (name = "dog_id ")
- Public class dog extends animal {
- @ Column (name = "Legs ")
- Private integer legs;
- // Omit Get Set
- }
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
- @ Entity
- @ Inheritance (Strategy = inheritancetype. table_per_class)
- @ Table (name = "t_vehicle ")
- Public class vehicle {// base class
- @ ID
- // @ Generatedvalue
- @ Column (name = "ID ")
- Private integer ID;
- @ Column (name = "Speed ")
- Private integer speed; // speed
- // Omit Get Set
- }
Java code
- @ Entity
- @ Table (name = "t_car ")
- Public class car extends vehicle {
- @ Column (name = "engine ")
- Private string engine; // Engine
- // Omit Get Set
- }
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