-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHibernate_Inheritance.txt
86 lines (70 loc) · 3.91 KB
/
Hibernate_Inheritance.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
Inheritance of objects and Hibernate:
Hibernate by-default implements a strategy for inheritance called as a "sigle table strategy". Which means it will create one common table for all inheritance hierarchy objects. Hibernate add one extra column DTYPE (descriminator type) which indicates what class it actually is
e.g. if TwoWheeler and FourWheeler extend Vehicle, then there will be only Vehicle table
+-------------+-----------+-----------------+----------------------+--------------------+
| DTYPE | vehicleId | vehicleName | steeringHandle | steeringWheel |
+-------------+-----------+-----------------+----------------------+--------------------+
| Vehicle | 1 | General Vehicle | NULL | NULL |
| TwoWheeler | 2 | bike | bike steering handle | NULL |
| FourWheeler | 3 | car | NULL | car steering wheel |
+-------------+-----------+-----------------+----------------------+--------------------+
Note: Single table strategy is the default startegy for inheritance in hibernate.
To explicitly configure the inheritance strategy we can add annotation @Inheritance on top of the base/parent entity.
e.g @Inheritance(strategy = InheritanceType.JOINED)
Three types of inheritance startegy are available:
1. JOINED
2. SINGLE_TABLE
3. TABLE_PER_CLASS
@DiscriminatorColumn(name = "VEHICLE_TYPE", discriminatorType = DiscriminatorType.STRING)
- on top of base/parent entity
- to configure the DTYPE column
@DiscriminatorValue("BIKE")
- on top of child entity
- to cofigure the value in descriminator type column to custom value, instead of default class name
TABLE_PER_CLASS strategy:
since all the object are getting stored in single table, some times queries can get very complex and involve costly performance when trying to perform a qurery on a specific type of class.
In those kind of scenarios, we can go for table per class startegy.
add annotation @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) on the base/parent entity
Note: in this strategy the members of the parent along withh their annotations also are inheirted in the table of the child class.
vehicle table
+-----------+-----------------+
| vehicleId | vehicleName |
+-----------+-----------------+
| 1 | General Vehicle |
+-----------+-----------------+
two wheeler table
+-----------+-------------+----------------------+
| vehicleId | vehicleName | steeringHandle |
+-----------+-------------+----------------------+
| 2 | bike | bike steering handle |
+-----------+-------------+----------------------+
four wheeler table
+-----------+-------------+--------------------+
| vehicleId | vehicleName | steeringWheel |
+-----------+-------------+--------------------+
| 3 | car | car steering wheel |
+-----------+-------------+--------------------+
JOINED strategy:
There will be sepearte table for each class, but all table will contain columns corresponding to specific class only. The indetifier/reference column is copied in all tables.
i.e. The fields/columns inherited from the base entity table does not go into the child entity. The child class specific fields/coloumns does not go to the base/parent table.
e.g.
vehicle:
+-----------+-----------------+
| vehicleId | vehicleName |
+-----------+-----------------+
| 1 | General Vehicle |
| 2 | bike |
| 3 | car |
+-----------+-----------------+
two wheeler table
+----------------------+-----------+
| steeringHandle | vehicleId |
+----------------------+-----------+
| bike steering handle | 2 |
+----------------------+-----------+
four wheeler table
+--------------------+-----------+
| steeringWheel | vehicleId |
+--------------------+-----------+
| car steering wheel | 3 |
+--------------------+-----------+