-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_per_class_hierarchy.txt
158 lines (86 loc) · 5.26 KB
/
table_per_class_hierarchy.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
For hbm2ddl.auto property the list of possible options is:
==========================================================
validate: validate that the schema matches, make no changes to the schema of the database, you probably want this for production.
update: update the schema to reflect the entities being persisted
create: creates the schema necessary for your entities, destroying any previous data.
create-drop: create the schema as in create above, but also drop the schema at the end of the session. This is great in early development or for testing.
Working with the table per class hierarchy strategy of inheritance
------------------------------------------------------------------
Java is an object-oriented programming language, and while working with the object-oriented paradigm, one thing comes to our mind: inheritance. We form a real-world scenario using IS A and HAS A relationships. Inheritance is supported by many languages, but relational databases are unable to understand the relationship of inheritance. Hibernate provides a way to map real-time relationships to the database.
Hibernate provides multiple strategies to achieve such a relationship for relational databases. There are three inheritance mapping strategies defined in hibernate:
1. Table per class hierarchy
2. Table per subclass
3. Table per concrete class
1.Table per class hierarchy
===========================
Here, we will create a new data structure that will help you understand the inheritance strategy.
Consider a class, Employee. We will extend the Employee class into two subclasses—PermanentEmployee and ContractualEmployee
Employee
---id, name
|----- PermanentEmployee
|---salary
|-----ContractualEmployee
|---hourlyRate
|---contractPeriod
Employee.java
--------------
@Entity
@Table(name="employee")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="emp_type" , discriminatorType=DiscriminatorType.STRING , length=2
)
@DiscriminatorValue(value="E")
public class Employee {
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="name")
private String name;
//getters and setters
}
PermanentEmployee.java
-----------------------
@Entity
@Table(name = "employee")
@DiscriminatorValue(value="PE")
public class PermanentEmployee extends Employee {
@Column(name="salary")
private Double salary;
//getters and setters
}
ContractualEmployee.java
------------------------
@Entity
@Table(name = "employee")
@DiscriminatorValue(value="CE")
public class ContractualEmployee extends Employee {
@Column(name="hourly_rate")
private Double HourlyRate;
@Column(name="contract_period")
private Float ContractPeriod;
//getters and setters
}
Now, hibernate will create a table with the fields of Employee, ContractualEmployee, PermanentEmployee, and one more column defined in the name attribute of the @DiscriminatorColumn annotation.
Here, the Employee class is the topmost in the hierarchy, and we used some annotation on the Employee class. Let's take a look at the annotations used in all three classes.
Annotations used in Employee.java
Following are the annotations used in Employee.java:
@Inheritance(strategy=InheritanceType.SINGLE_TABLE): This annotation is used to define the inheritance strategy. It is used only on the root class in the hierarchy.
@DiscriminatorColumn(name="emp_type", discriminatorType=DiscriminatorType.STRING, length=2): This annotation is used to define the discriminator column for the SINGLE_TABLE and JOINED mapping strategies. The attributes are as follows:
name="emp_type": hibernate creates a column with the value provided for the name attribute
discriminatorType=DiscriminatorType.STRING: This is used to define the datatype of the discriminator column
length=2: This is used to define the field size of the discriminator column
@DiscriminatorValue(value="E"): This annotation defines the value of the discriminator column for this particular class. If the value is not provided, hibernate uses a class name in DiscriminatorType.STRING, and the provided specific functions will be used otherwise.
Annotations used in ContractualEmployee.java
Following are the annotations used in ContractualEmployee.java:
@DiscriminatorValue(value="CE"): The value "CE" is used for this particular class
Annotations used in PermanentEmployee.java
Following are the annotations used in PermanentEmployee.java:
@DiscriminatorValue(value="PE"): The value "PE" is used for this particular class
How it works…
=============
Here, we defined the Employee class as the parent class, and the ContractualEmployee and PermanentEmployee classes are defined as the subclasses of the Employee class.
When we save a record in the parent class, hibernate saves the values in the fields of that particular class and the other columns from the subclasses are saved with a null value.
If we save the record in the ContractualEmployee and PermanentEmployee subclasses, hibernate saves the values in the fields of the current and parent class.
We can use the value of the emp_type column, which is E (Employee), PE is for PermanentEmployee and CE for ContractualEmployee to determine records.