﻿WEBVTT

1
00:00:01.303 --> 00:00:02.136
<v Instructor>In this video,</v>

2
00:00:02.136 --> 00:00:06.136
we'll cover Hibernate one-to one bi-directional.

3
00:00:07.755 --> 00:00:11.036
So, we currently have a uni-directional mapping

4
00:00:11.036 --> 00:00:14.875
between our Instructor and our InstructorDetail

5
00:00:14.875 --> 00:00:16.681
and that's working just fine for us.

6
00:00:16.681 --> 00:00:20.875
We covered all of that in the previous video

7
00:00:20.875 --> 00:00:23.263
but now we have a new use case.

8
00:00:23.263 --> 00:00:27.218
Basically we wanna load an InstructorDetail object

9
00:00:27.218 --> 00:00:30.606
and then we'd like to get the associated Instructor

10
00:00:30.606 --> 00:00:32.859
for that detail object

11
00:00:32.859 --> 00:00:34.750
but we can't really do this

12
00:00:34.750 --> 00:00:38.376
with our current uni-directional relationship,

13
00:00:38.376 --> 00:00:41.327
so right now we can only start with Instructor

14
00:00:41.327 --> 00:00:43.465
and then move to InstructorDetail,

15
00:00:43.465 --> 00:00:45.407
we can't go the other way.

16
00:00:45.407 --> 00:00:49.574
With uni-directional it's really just a one-way street.

17
00:00:50.426 --> 00:00:51.981
But we can actually solve this

18
00:00:51.981 --> 00:00:55.309
by making use of a bi-directional relationship.

19
00:00:55.309 --> 00:00:58.659
So what we can do is start with the InstructorDetail

20
00:00:58.659 --> 00:01:00.866
and then we can actually make it back

21
00:01:00.866 --> 00:01:02.552
to the instructor

22
00:01:02.552 --> 00:01:03.991
and also since we already have

23
00:01:03.991 --> 00:01:06.719
the previous uni-directional relationship in place,

24
00:01:06.719 --> 00:01:08.858
we effectively have bi-directional now,

25
00:01:08.858 --> 00:01:10.978
so now this is a two-way street,

26
00:01:10.978 --> 00:01:12.719
so we can start on any side.

27
00:01:12.719 --> 00:01:14.396
We can start with instructor,

28
00:01:14.396 --> 00:01:15.676
get the InstructorDetail

29
00:01:15.676 --> 00:01:18.438
or start with InstructorDetail

30
00:01:18.438 --> 00:01:21.355
and make it back to the instructor.

31
00:01:22.407 --> 00:01:24.697
So, the nice thing about using bi-directional

32
00:01:24.697 --> 00:01:26.746
is that we have some good news.

33
00:01:26.746 --> 00:01:30.308
We can actually keep our existing database schema

34
00:01:30.308 --> 00:01:34.450
so this is really cool so there's really no changes required

35
00:01:34.450 --> 00:01:35.872
to our database setup,

36
00:01:35.872 --> 00:01:38.012
so we can continue to use the Instructor,

37
00:01:38.012 --> 00:01:39.434
InstructorDetail table

38
00:01:39.434 --> 00:01:41.027
with the foreign keys that we set up

39
00:01:41.027 --> 00:01:43.330
in the previous video.

40
00:01:43.330 --> 00:01:45.855
So, really the only thing that we have to change now

41
00:01:45.855 --> 00:01:48.219
is to simple update the Java code

42
00:01:48.219 --> 00:01:51.552
and we'll cover that part in this video.

43
00:01:54.398 --> 00:01:56.381
Alrighty, so here's our development process,

44
00:01:56.381 --> 00:01:58.908
so we basically need to make some updates

45
00:01:58.908 --> 00:02:01.745
to the InstructorDetail class.

46
00:02:01.745 --> 00:02:05.500
We'll need to add a new field to reference the Instructor

47
00:02:05.500 --> 00:02:07.783
and also add the appropriate getter/setter methods

48
00:02:07.783 --> 00:02:10.493
for Instructor and then we need to add

49
00:02:10.493 --> 00:02:14.523
a OneToOne annotation such that we can point back

50
00:02:14.523 --> 00:02:17.395
to the original Instructor

51
00:02:17.395 --> 00:02:19.964
and then finally, we'll kind of pull it all together

52
00:02:19.964 --> 00:02:21.672
by creating a main application

53
00:02:21.672 --> 00:02:24.255
and we'll test all of this out.

54
00:02:26.369 --> 00:02:28.457
Alrighty, so let's start with step 1.1,

55
00:02:28.457 --> 00:02:32.006
adding a new field to reference Instructor.

56
00:02:32.006 --> 00:02:34.663
So, here's our class here InstructorDetail

57
00:02:34.663 --> 00:02:36.463
and we have the code in place right now

58
00:02:36.463 --> 00:02:38.136
for adding that new field,

59
00:02:38.136 --> 00:02:41.469
so we have a new field named Instructor.

60
00:02:42.349 --> 00:02:45.244
Alright, so step 1.2 add in getter/setter methods

61
00:02:45.244 --> 00:02:47.015
for Instructor, so here we just have

62
00:02:47.015 --> 00:02:48.413
a getter and a setter.

63
00:02:48.413 --> 00:02:50.412
Again, there's no rocket science here.

64
00:02:50.412 --> 00:02:53.389
Basically we can generate this with Eclipse

65
00:02:53.389 --> 00:02:55.828
but the key thing here is that we can call the setter method

66
00:02:55.828 --> 00:02:58.451
for adding an Instructor or setting the instructor,

67
00:02:58.451 --> 00:03:01.156
use the getter method for getting the associated Instructor

68
00:03:01.156 --> 00:03:04.656
with this given InstructorDetail instance.

69
00:03:06.627 --> 00:03:08.355
Now we're moving ahead to step 1.3.

70
00:03:08.355 --> 00:03:11.638
This is where we add the OneToOne annotation.

71
00:03:11.638 --> 00:03:14.610
So, note here on our field for Instructor,

72
00:03:14.610 --> 00:03:16.277
we add the @OneToOne

73
00:03:17.245 --> 00:03:19.328
and then we have a new entry here

74
00:03:19.328 --> 00:03:21.241
that's a little interesting.

75
00:03:21.241 --> 00:03:24.221
We say mappedBy InstructorDetail.

76
00:03:24.221 --> 00:03:27.325
So, what exactly does this refer to

77
00:03:27.325 --> 00:03:28.675
or what does this mean?

78
00:03:28.675 --> 00:03:33.299
Well, this basically refers to the InstructorDetail property

79
00:03:33.299 --> 00:03:35.341
in the Instructor class.

80
00:03:35.341 --> 00:03:36.439
So, what we're telling Hibernate

81
00:03:36.439 --> 00:03:38.483
is that this Instructor field

82
00:03:38.483 --> 00:03:42.548
is actually mapped by the InstructorDetail property

83
00:03:42.548 --> 00:03:45.014
in the Instructor class.

84
00:03:45.014 --> 00:03:47.139
Alright, so that's kind of the mappedBy.

85
00:03:47.139 --> 00:03:51.155
Let's kind of dig into this a little further.

86
00:03:51.155 --> 00:03:52.636
So, a bit more on mappedBy.

87
00:03:52.636 --> 00:03:55.683
So, we have this reference here for mappedBy.

88
00:03:55.683 --> 00:03:58.214
That's in our InstructorDetail class here.

89
00:03:58.214 --> 00:04:00.519
So again, the mappedBy tells Hibernate

90
00:04:00.519 --> 00:04:03.536
to look at the InstructorDetail property

91
00:04:03.536 --> 00:04:05.691
in the Instructor class

92
00:04:05.691 --> 00:04:08.587
so Hibernate can actually use the information

93
00:04:08.587 --> 00:04:11.664
from the Instructor class's join column

94
00:04:11.664 --> 00:04:16.217
to figure out how to find the associated Instructor,

95
00:04:16.217 --> 00:04:18.288
alright, so you're basically telling Hibernate

96
00:04:18.288 --> 00:04:22.932
hey, this field Instructor in InstructorDetail

97
00:04:22.932 --> 00:04:25.276
it's mappedBy InstructorDetail.

98
00:04:25.276 --> 00:04:26.441
Hibernate will go figure that out,

99
00:04:26.441 --> 00:04:28.344
look at the foreign key relationship

100
00:04:28.344 --> 00:04:29.546
and match everything up,

101
00:04:29.546 --> 00:04:32.306
so effectively Hibernate can use this information

102
00:04:32.306 --> 00:04:34.680
to find out how these two items are linked up

103
00:04:34.680 --> 00:04:37.519
and also finding the appropriate Instructor

104
00:04:37.519 --> 00:04:40.186
for this given InstructorDetail.

105
00:04:42.680 --> 00:04:44.018
Alrighty, so that's the mappedBy.

106
00:04:44.018 --> 00:04:46.116
Now, the other thing here is cascading,

107
00:04:46.116 --> 00:04:47.969
so we'll add support for cascading,

108
00:04:47.969 --> 00:04:50.263
so this new entry that I just added here,

109
00:04:50.263 --> 00:04:53.162
cascade equals CascadeType.ALL,

110
00:04:53.162 --> 00:04:55.143
so remember from the previous videos,

111
00:04:55.143 --> 00:04:58.325
this will actually cascade all operations

112
00:04:58.325 --> 00:05:00.339
to the associated Instructor.

113
00:05:00.339 --> 00:05:03.290
So, if I load an InstructorDetail

114
00:05:03.290 --> 00:05:05.839
then if I wanna delete that InstructorDetail,

115
00:05:05.839 --> 00:05:08.131
this will also cascade the delete operation

116
00:05:08.131 --> 00:05:10.197
to that given Instructor

117
00:05:10.197 --> 00:05:12.493
and remember, you have fine grained control

118
00:05:12.493 --> 00:05:14.077
over the cascading types

119
00:05:14.077 --> 00:05:16.065
so if you don't want to cascade all,

120
00:05:16.065 --> 00:05:17.971
you can choose the appropriate cascade types

121
00:05:17.971 --> 00:05:22.282
that you'd like to use for your given use case.

122
00:05:22.282 --> 00:05:23.995
Alright, so we can kind of wrap it up here

123
00:05:23.995 --> 00:05:26.188
and we can wrap it up by creating a main app,

124
00:05:26.188 --> 00:05:27.445
that's our step two.

125
00:05:27.445 --> 00:05:29.090
So, basically what we're gonna do here

126
00:05:29.090 --> 00:05:30.114
is that we're gonna go through

127
00:05:30.114 --> 00:05:33.413
and actually get the InstructorDetail object

128
00:05:33.413 --> 00:05:34.638
from the database,

129
00:05:34.638 --> 00:05:36.685
so I'll set up theId equals one,

130
00:05:36.685 --> 00:05:38.973
that's whatever the ID is

131
00:05:38.973 --> 00:05:41.788
or primary key for that given InstructorDetail object

132
00:05:41.788 --> 00:05:44.261
and then I'll go and grab that InstructorDetail

133
00:05:44.261 --> 00:05:45.115
from the database,

134
00:05:45.115 --> 00:05:48.491
so I do a session.get InstructorDetail.class

135
00:05:48.491 --> 00:05:51.180
and I pass in a reference to theId.

136
00:05:51.180 --> 00:05:52.846
Then what I'll do is kind of move forward here

137
00:05:52.846 --> 00:05:54.249
and I'll print out the details,

138
00:05:54.249 --> 00:05:56.754
so I'll say system.out.print line,

139
00:05:56.754 --> 00:05:58.504
tempInstructorDetail.

140
00:05:59.543 --> 00:06:00.892
And then we'll go through and print out

141
00:06:00.892 --> 00:06:02.007
the associated Instructor.

142
00:06:02.007 --> 00:06:03.764
So, here I'd do a system.out.print line,

143
00:06:03.764 --> 00:06:07.125
and I'll say tempInstructorDetail.getInstructor.

144
00:06:07.125 --> 00:06:10.522
So, remember, now we have this bi-directional relationship,

145
00:06:10.522 --> 00:06:12.697
so by going to the InstructorDetail,

146
00:06:12.697 --> 00:06:15.728
I can get back to the actual Instructor

147
00:06:15.728 --> 00:06:17.597
and print that information out.

148
00:06:17.597 --> 00:06:20.248
So, this is great, so this kind of meets our objectives,

149
00:06:20.248 --> 00:06:22.287
kind of fulfills our requirement

150
00:06:22.287 --> 00:06:24.804
of having this bi-directional relationship

151
00:06:24.804 --> 00:06:28.137
between Instructor and InstructorDetail.

152
00:06:29.345 --> 00:06:31.088
Alrighty, so this is some good information.

153
00:06:31.088 --> 00:06:32.478
What we're gonna do in the next video,

154
00:06:32.478 --> 00:06:34.955
jump into Eclipse and then we'll write this code

155
00:06:34.955 --> 00:06:35.938
and we'll test it out,

156
00:06:35.938 --> 00:06:38.855
so I'll see you in the next video.

