-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_per_subclass_strategy.txt
131 lines (80 loc) · 3.14 KB
/
table_per_subclass_strategy.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Working with the table per subclass strategy of inheritance
------------------------------------------------------------
we went through the table per class hierarchy inheritance strategy. Table per class hierarchy stores all the rows in a single table and the discriminator column is used to uniquely identify the records. Sometimes, the tables become very large if the hierarchy is deep. In such a case, we can use another strategy called table per subclass.
In the table-per-subclass strategy, hibernate creates separate tables for each class. The relationship exists between the parent and child tables, where the common data is stored in the parent class and the data of the subclass is stored in a separate specific table.
Employee
---id
---name
|----- PermanentEmployee
---emp_id
---salary
|-----ContractualEmployee
---emp_id
---hourlyRate
---contractPeriod
Employee.java
--------------
@Entity
@Table(name="employee")
@Inheritance(strategy=InheritanceType.JOINED)
public class Employee {
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="name")
private String name;
// getters and setters
}
ContractualEmployee.java
--------------------------
@Entity
@Table
@PrimaryKeyJoinColumn(name="emp_id")
public class ContractualEmployee extends Employee {
@Column(name="hourly_rate")
private Double HourlyRate;
@Column(name="contract_period")
private Float ContractPeriod;
// getters and setters
}
PermanentEmployee.java
----------------------
@Entity
@Table
@PrimaryKeyJoinColumn(name="emp_id")
public class PermanentEmployee extends Employee {
@Column(name="salary")
private Double salary;
// getters and setters
}
Tables:
--------
CREATE TABLE `employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `contractualemployee` (
`contract_period` float DEFAULT NULL,
`hourly_rate` double DEFAULT NULL,
`emp_id` bigint(20) NOT NULL,
PRIMARY KEY (`emp_id`),
KEY `FK_EMPLOYEE_ID` (`emp_id`),
CONSTRAINT `FK_EMPLOYEE_ID`
FOREIGN KEY (`emp_id`)
REFERENCES `employee` (`id`)
);
CREATE TABLE `permanentemployee` (
`salary` double DEFAULT NULL,
`emp_id` bigint(20) NOT NULL,
PRIMARY KEY (`emp_id`),
KEY `FK_EMPLOYEE_ID` (`emp_id`),
CONSTRAINT `_EMPLOYEE_ID`
FOREIGN KEY (`emp_id`)
REFERENCES `employee` (`id`)
);
In this strategy, we used the @Inheritance annotation with strategy=InheritanceType.JOINED in the Employee parent class only. For the subclasses, we used @PrimaryKeyJoinColumn(name="emp_id"). Hibernate will create a foreign key column in all the subtables with the value of the name attribute of the @PrimaryKeyJoinColumn annotation.
How it works…
--------------
Upon careful observation of the data from the three tables and its output, you can understand how this strategy works. In this strategy, the common data is stored in the parent table, which is Employee here. The subtable stores the class-specific and common data in the parent table. Also, the subclasses refer to the parent class primary key as a foreign key.