Employee. Java: (base class)
1 @ Entity
2 @ Inheritance (Strategy = inheritancetype. single_table) // Select an inheritance Policy
3 @ Discriminatorcolumn (name = "type ") // Configure the authenticator
4 @ Discriminatorvalue ("0 ") // Set the authenticator Value
5 Public Class Employee {
6 @ ID
7 @ Generatedvalue
8 Private Integer ID;
9 Private String name;
10 Public Integer GETID (){
11 Return ID;
12 }
13 Public Void Setid (integer ID ){
14 This . ID = ID;
15 }
16 Public String getname (){
17 Return Name;
18 }
19 Public Void Setname (string name ){
20 This . Name = Name;
21 }
22 }
Where@ Inheritance (Strategy = inheritancetype. single_table) // select an inheritance PolicyYou can select multiple policies, as shown in:
- Joined: the inherited child classes generate a table.
- Single_table: generate only one table and use one field as the authenticator
- Table_per_class: each class produces a table, including the base class.
Skiller. Java (inherited from employee. Java)
1 @ Entity
2 @ Discriminatorvalue ("1 ")
3 Public Class Skiller Extends Employee {
4 Private String skill;
5
6 Public String getskill (){
7 Return Skill;
8 }
9
10 Public Void Setskill (string skill ){
11 This . Skill = skill;
12 }
13 }
Sales. Java (inherited from employee. Java)
1 @ Entity
2 @ Discriminatorvalue ("2 ")
3 Public Class Sales Extends Employee {
4 Private String delimiter;
5 Public String getask (){
6 Return Break;
7 }
8 Public Void Setparts (string parts ){
9 This . Bytes = bytes;
10 }
11 }
Test:
1 @ Test
2 Public Void Save (){
3 Entitymanagerfactory factory = persistence. createentitymanagerfactory ("sample ");
4 Entitymanager em = factory. createentitymanager ();
5 Em. gettransaction (). Begin ();
6 Employee p1 = New Employee ();
7 P1.setname ("Jim ");
8 Skiller P2 = New Skiller ();
9 P2.setname ("hanson ");
10 P2.setskill ("skill ");
11 Sales P3 = New Sales ();
12 P3.setname ("Brian ");
13 P3.setvalues ("sale ");
14 Em. persist (P1 );
15 Em. persist (P2 );
16 Em. persist (P3 );
17 Em. gettransaction (). Commit ();
18 Em. Close ();
19 Factory. Close ();
20 }
Open the database: