1
00:00:00,930 --> 00:00:03,330
Instructor: Welcome back.

2
00:00:03,330 --> 00:00:05,103
We have the Todo bean.

3
00:00:06,360 --> 00:00:09,960
Let's say we have a Todo table in the database.

4
00:00:09,960 --> 00:00:14,493
JPA allows us to map your bean to the database.

5
00:00:15,690 --> 00:00:16,680
The way you can do that

6
00:00:16,680 --> 00:00:21,093
is by adding a simple annotation called @Entity.

7
00:00:22,530 --> 00:00:25,150
If you say the Todo is an entity

8
00:00:26,400 --> 00:00:31,400
it means that this bean is mapped to a database table.

9
00:00:31,920 --> 00:00:34,800
Let's just add in entity, Ctrl + 1,

10
00:00:34,800 --> 00:00:39,540
input jakarta.persistence.

11
00:00:39,540 --> 00:00:43,233
So you'd see that jakarta.persistence.Entity is imported in.

12
00:00:45,600 --> 00:00:47,700
Whenever you have a entity

13
00:00:47,700 --> 00:00:51,360
you would also need to have a ID.

14
00:00:51,360 --> 00:00:53,163
So you need to define primary key.

15
00:00:54,330 --> 00:00:57,693
I'll add in @Id and say Ctrl + 1,

16
00:00:58,650 --> 00:01:01,110
import ID jakarta.persistence.

17
00:01:01,110 --> 00:01:01,943
Let's do that.

18
00:01:01,943 --> 00:01:04,680
And we'd also want to generate this using a sequence.

19
00:01:04,680 --> 00:01:06,080
So I'll say @GeneratedValue.

20
00:01:08,250 --> 00:01:10,710
So think about what we are doing in here.

21
00:01:10,710 --> 00:01:13,560
We are mapping Todo bean, this bean,

22
00:01:13,560 --> 00:01:16,650
to a table in the database.

23
00:01:16,650 --> 00:01:19,830
That's what entities allow you Todo.

24
00:01:19,830 --> 00:01:23,310
It allows you to map a bean to a database table.

25
00:01:23,310 --> 00:01:25,980
One of the magic of Spring Boot auto-configuration

26
00:01:25,980 --> 00:01:28,950
is that if it sees any entities

27
00:01:28,950 --> 00:01:32,793
then it would automatically start creating tables in H2.

28
00:01:33,630 --> 00:01:37,893
If you look at our pom.xml, we have H2 database in here.

29
00:01:38,880 --> 00:01:40,263
So let's see where it is.

30
00:01:41,670 --> 00:01:46,530
If you look at pom.xml, we have H2 database in here.

31
00:01:46,530 --> 00:01:49,530
And as soon as Spring Boot sees H2 database

32
00:01:49,530 --> 00:01:53,400
what it does is it would start pre-configuring it.

33
00:01:53,400 --> 00:01:57,870
One of the pre-configuring steps is if it sees any entities,

34
00:01:57,870 --> 00:02:00,180
if it sees any entities like this

35
00:02:00,180 --> 00:02:03,210
it would directly start creating tables for them.

36
00:02:03,210 --> 00:02:07,590
Let's see that in action. Let's go to H2 console.

37
00:02:07,590 --> 00:02:12,540
Let's refresh. Let's re-log in by connecting.

38
00:02:12,540 --> 00:02:15,150
Make sure that you're using the right JDBC URL

39
00:02:15,150 --> 00:02:16,560
and say connect.

40
00:02:16,560 --> 00:02:19,710
Now you can see that there is a Todo table in here.

41
00:02:19,710 --> 00:02:20,820
So what's happening in here

42
00:02:20,820 --> 00:02:24,690
is that Spring Boot auto-configuration, whenever it sees H2

43
00:02:24,690 --> 00:02:27,660
or any in-memory database in the class path

44
00:02:27,660 --> 00:02:32,250
it'll directly create tables for all the entities.

45
00:02:32,250 --> 00:02:34,020
When you're defining your entity

46
00:02:34,020 --> 00:02:35,940
you have a lot of flexibility.

47
00:02:35,940 --> 00:02:39,330
Let's say the name of the table I would want to map to

48
00:02:39,330 --> 00:02:41,820
is not Todo, but let's say I would want to map

49
00:02:41,820 --> 00:02:43,410
to something else.

50
00:02:43,410 --> 00:02:48,363
So I would say name is equal to TodoABC and save.

51
00:02:52,080 --> 00:02:53,940
Let's refresh this.

52
00:02:53,940 --> 00:02:55,623
Continue. Connect.

53
00:02:56,640 --> 00:03:00,330
You can see that the name of the table is now TodoABC.

54
00:03:00,330 --> 00:03:05,190
So we are mapping to a table called TodoABC.

55
00:03:05,190 --> 00:03:07,410
By default it'll use the class name itself.

56
00:03:07,410 --> 00:03:08,730
I'll remove this.

57
00:03:08,730 --> 00:03:09,840
You also have flexibility

58
00:03:09,840 --> 00:03:12,540
in terms of customizing the column names as well.

59
00:03:12,540 --> 00:03:14,460
Instead of calling this USERNAME,

60
00:03:14,460 --> 00:03:17,010
let's say I would want to give it a different name.

61
00:03:17,010 --> 00:03:20,943
I can say column and give it a name.

62
00:03:21,840 --> 00:03:24,060
A name is equal to name.

63
00:03:24,060 --> 00:03:27,300
Right now if you look at this, it's called USERNAME.

64
00:03:27,300 --> 00:03:32,300
But if I save this and come back to H2 console,

65
00:03:33,120 --> 00:03:35,163
refresh it and connect it back,

66
00:03:36,210 --> 00:03:39,543
you would see that the name is now name.

67
00:03:40,950 --> 00:03:43,533
So this field is now mapped to this.

68
00:03:44,490 --> 00:03:47,640
JPA also supports mapping complex relationships.

69
00:03:47,640 --> 00:03:49,440
One-to-one, one-to-many, many-to-one,

70
00:03:49,440 --> 00:03:51,660
many-to-many and things like that.

71
00:03:51,660 --> 00:03:53,250
For now, let's remove the column.

72
00:03:53,250 --> 00:03:56,070
So I'll remove this column name is equal to name.

73
00:03:56,070 --> 00:03:58,320
I would want to continue using the default values

74
00:03:58,320 --> 00:03:59,190
which are generated.

75
00:03:59,190 --> 00:04:03,093
Make sure that the entity name is also Todo and save it out.

76
00:04:04,080 --> 00:04:08,880
And when I actually refresh this, go back and say connect,

77
00:04:08,880 --> 00:04:12,780
I should see Todo table in here with the usual column names.

78
00:04:12,780 --> 00:04:16,740
ID, DESCRIPTION, DONE, TARGET_DATE, USERNAME.

79
00:04:16,740 --> 00:04:17,913
So these look good.

80
00:04:18,930 --> 00:04:21,079
One thing you would observe is TARGET_DATE.

81
00:04:23,040 --> 00:04:26,700
The reason is because there is a init cap in here.

82
00:04:26,700 --> 00:04:28,320
So this is in caps.

83
00:04:28,320 --> 00:04:32,310
So what happens is in a database typically

84
00:04:32,310 --> 00:04:34,800
we separate words using underscore.

85
00:04:34,800 --> 00:04:36,450
We capitalized D in here,

86
00:04:36,450 --> 00:04:40,893
that's why the column name is created as TARGET_DATE.

87
00:04:46,200 --> 00:04:49,293
Let's organize imports and save this.

88
00:04:51,330 --> 00:04:54,900
Now I would want to populate some data to start off.

89
00:04:54,900 --> 00:04:57,783
So how can I populate some data to start off?

90
00:04:59,760 --> 00:05:03,375
I can create a file called data.sql.

91
00:05:03,375 --> 00:05:08,375
So src/main/resources, new, SQL file, data.sql.

92
00:05:12,360 --> 00:05:15,720
In the data.sql I can create queries

93
00:05:15,720 --> 00:05:18,453
and these queries will be executed at startup.

94
00:05:19,560 --> 00:05:22,593
So I'll say insert into todos.

95
00:05:26,190 --> 00:05:27,693
Actually, insert into todo.

96
00:05:28,830 --> 00:05:32,073
Values, and you can specify the values in here.

97
00:05:35,670 --> 00:05:37,800
The default GeneratedValues start with one,

98
00:05:37,800 --> 00:05:40,203
so I'll use 10,001 as the ID.

99
00:05:44,070 --> 00:05:46,140
The second one is DESCRIPTION.

100
00:05:46,140 --> 00:05:51,140
I'll say learn, or you can say Get AWS Certified.

101
00:06:02,940 --> 00:06:05,333
The next one is DONE, which is false.

102
00:06:09,300 --> 00:06:11,100
The order of these does not look good.

103
00:06:11,100 --> 00:06:15,030
So what I do is I would define my own columns in here.

104
00:06:15,030 --> 00:06:17,790
So I would want to insert in the right order.

105
00:06:17,790 --> 00:06:21,210
So I would want to define my own thing in here.

106
00:06:21,210 --> 00:06:25,023
So I would say ID, first ID, then DESCRIPTION,

107
00:06:26,010 --> 00:06:28,320
then I would want to insert...

108
00:06:28,320 --> 00:06:31,220
Actually, before DESCRIPTION we need to have the USERNAME.

109
00:06:33,480 --> 00:06:35,850
ID, USERNAME, DESCRIPTION,

110
00:06:35,850 --> 00:06:37,963
after that would be the TARGET_DATE.

111
00:06:39,300 --> 00:06:41,010
And the last one is DONE.

112
00:06:41,010 --> 00:06:43,470
So let's insert values in the same way.

113
00:06:43,470 --> 00:06:45,540
So let's maximize this.

114
00:06:45,540 --> 00:06:49,403
And 10,001, username is in28minutes or Ranga.

115
00:06:51,600 --> 00:06:53,280
You can insert for any of those users

116
00:06:53,280 --> 00:06:55,050
and use that specific user.

117
00:06:55,050 --> 00:06:56,670
Get AWS certified.

118
00:06:56,670 --> 00:07:00,660
The target date, let's use a H2 function

119
00:07:00,660 --> 00:07:02,073
called current date.

120
00:07:03,030 --> 00:07:07,113
And is it done? It's false.

121
00:07:08,520 --> 00:07:09,663
And save this.

122
00:07:13,140 --> 00:07:17,070
If you go to console right now, you'd see an error.

123
00:07:17,070 --> 00:07:19,980
It's saying table TODO not found.

124
00:07:19,980 --> 00:07:23,730
By default data.sql is executed

125
00:07:23,730 --> 00:07:26,640
before the entities are processed.

126
00:07:26,640 --> 00:07:29,313
Tables are created when the entities are processed.

127
00:07:30,540 --> 00:07:35,540
So data.sql is executed before the table is created

128
00:07:36,210 --> 00:07:37,950
and we would want to change that.

129
00:07:37,950 --> 00:07:39,330
How do we change that?

130
00:07:39,330 --> 00:07:40,920
That's where we can make a change

131
00:07:40,920 --> 00:07:42,723
in the application.properties.

132
00:07:44,880 --> 00:07:47,550
That's something we can configure in application.properties.

133
00:07:47,550 --> 00:07:50,310
So let's open up application.properties

134
00:07:50,310 --> 00:07:55,310
and type in spring.jpa.defer-datasource-initialization.

135
00:08:02,910 --> 00:08:04,260
Make sure that you get the spelling right.

136
00:08:04,260 --> 00:08:08,260
It's spring.jpa.defer-datasource-initialization

137
00:08:09,240 --> 00:08:12,423
is equal to true, and say save.

138
00:08:13,710 --> 00:08:16,290
When I stopped and started the application again

139
00:08:16,290 --> 00:08:18,210
I saw a syntax exception.

140
00:08:18,210 --> 00:08:21,330
It says column in28minutes not found.

141
00:08:21,330 --> 00:08:22,800
The reason why it's thrown an error

142
00:08:22,800 --> 00:08:27,450
is because in data.sql you should use single quotes.

143
00:08:27,450 --> 00:08:30,750
So let me replace all these with single quotes.

144
00:08:30,750 --> 00:08:33,400
So instead of double quotes, let's use single quotes.

145
00:08:40,260 --> 00:08:41,222
And save this.

146
00:08:45,450 --> 00:08:49,563
Let's see it in action now, let's start it again.

147
00:08:50,520 --> 00:08:53,040
And now, everything looks hunky dory.

148
00:08:53,040 --> 00:08:54,840
The application has started up fine.

149
00:08:55,770 --> 00:08:59,613
Let's go to H2 console, refresh and connect again.

150
00:09:00,690 --> 00:09:03,903
TODO table, do you have the data?

151
00:09:04,800 --> 00:09:06,843
Yep, we have the data in here.

152
00:09:08,280 --> 00:09:11,647
Let's add a few more todos to data.sql.

153
00:09:14,360 --> 00:09:19,360
Ctrl + A. So let's add a few more ID.

154
00:09:19,860 --> 00:09:22,110
Should be increased. You cannot have duplicate IDs.

155
00:09:22,110 --> 00:09:25,003
So 10,003, 10,004.

156
00:09:26,160 --> 00:09:28,830
I'll add all the todos for in28minutes itself.

157
00:09:28,830 --> 00:09:30,900
We'll be making use of in28minutes ID.

158
00:09:30,900 --> 00:09:33,720
If you want, you can make use of the Ranga ID as well.

159
00:09:33,720 --> 00:09:37,350
So let's say get Azure certified, GCP certified,

160
00:09:37,350 --> 00:09:40,920
and let's say learn full stack or DevOps,

161
00:09:40,920 --> 00:09:42,360
whatever you'd want to do.

162
00:09:42,360 --> 00:09:44,070
Or learn to dance, right?

163
00:09:44,070 --> 00:09:47,130
So whatever you'd want to do, you can put them in here.

164
00:09:47,130 --> 00:09:50,343
Make sure that the ideas are unique and save them.

165
00:09:52,500 --> 00:09:57,500
And go back and refresh, connect, and TODO, run.

166
00:10:01,080 --> 00:10:02,130
One thing you need to remember

167
00:10:02,130 --> 00:10:04,680
is that even H2 is an in-memory database.

168
00:10:04,680 --> 00:10:06,480
When you restart the application,

169
00:10:06,480 --> 00:10:09,420
all the data in H2 will be lost.

170
00:10:09,420 --> 00:10:11,640
In the step, we got the H2 database ready.

171
00:10:11,640 --> 00:10:14,040
We have the table ready, we have the data ready

172
00:10:14,040 --> 00:10:17,490
and now we would want to display these todos

173
00:10:17,490 --> 00:10:19,410
connecting to the H2 database.

174
00:10:19,410 --> 00:10:21,990
We'd want to use JPA, connect to the database,

175
00:10:21,990 --> 00:10:23,790
and show the todos in here.

176
00:10:23,790 --> 00:10:26,010
And we would want to use JPA

177
00:10:26,010 --> 00:10:27,570
to perform all the operations as well.

178
00:10:27,570 --> 00:10:31,230
Delete, update, add, and a wide variety of them.

179
00:10:31,230 --> 00:10:34,053
Let's get started with that starting the next step.


