1
00:00:00,930 --> 00:00:02,700
Welcome back.

2
00:00:03,270 --> 00:00:05,160
We have the to do been.

3
00:00:06,300 --> 00:00:09,300
Let's say we have a to do table in the database.

4
00:00:09,840 --> 00:00:14,490
JPA allows us to map your bean to the database.

5
00:00:15,690 --> 00:00:21,150
The way you can do that is by adding a simple annotation called Add Entity.

6
00:00:22,500 --> 00:00:31,680
If you say the To-Do is an entity, it means that this bean is mapped to a database table.

7
00:00:31,740 --> 00:00:35,490
Let's just add in entity control one input.

8
00:00:36,700 --> 00:00:38,980
Jakarta persistence.

9
00:00:39,400 --> 00:00:43,240
So you'd see that Jakarta persistence not entities imported in.

10
00:00:45,490 --> 00:00:51,200
Whenever you have an entity, you would also need to have ID.

11
00:00:51,340 --> 00:00:53,230
So you need to define primary key.

12
00:00:54,300 --> 00:00:57,660
I'll add in at ID and say control one.

13
00:00:58,650 --> 00:00:59,210
Import it.

14
00:00:59,220 --> 00:01:00,810
Jakarta Persistence.

15
00:01:01,020 --> 00:01:01,800
Let's do that.

16
00:01:01,800 --> 00:01:04,680
And we would also want to generate this using a sequence.

17
00:01:04,690 --> 00:01:07,050
So I'll say generated value.

18
00:01:08,150 --> 00:01:10,280
So think about what we are doing in here.

19
00:01:10,610 --> 00:01:16,100
We are mapping to do been this been to a table in the database.

20
00:01:16,550 --> 00:01:19,310
That's what entities allow you to do.

21
00:01:19,790 --> 00:01:23,150
It allows you to map a bean to a database table.

22
00:01:23,180 --> 00:01:30,590
One of the magic of Springboard auto configuration is that if it sees any entities, then it would automatically

23
00:01:30,590 --> 00:01:32,990
start creating tables in H2.

24
00:01:33,560 --> 00:01:38,030
If you look at our Android XML, we have two database in here.

25
00:01:38,810 --> 00:01:40,310
So let's see where it is.

26
00:01:41,660 --> 00:01:45,980
If you look at Palm that XML, we have a database in here.

27
00:01:46,430 --> 00:01:53,150
And as soon as Springboard sees history database, what it does is it would start pre configuring it.

28
00:01:53,240 --> 00:01:57,770
One of the pre configuring steps is if it sees any entities.

29
00:01:57,800 --> 00:02:03,080
If it sees any entities like this, it would directly start creating tables for them.

30
00:02:03,110 --> 00:02:04,470
Let's see that in action.

31
00:02:04,490 --> 00:02:07,130
Let's go to Edge to console.

32
00:02:07,460 --> 00:02:09,259
Let's refresh.

33
00:02:10,300 --> 00:02:12,500
Let's re login by connecting.

34
00:02:12,520 --> 00:02:16,000
Make sure that you are using the right JDBC URL and say connect.

35
00:02:16,450 --> 00:02:19,210
Now you can see that there is a To-Do table in here.

36
00:02:19,600 --> 00:02:23,170
So what's happening in here is that Springboard auto configuration.

37
00:02:23,170 --> 00:02:30,490
Whenever it sees H2 or any in-memory database in the class path, it will directly create tables for

38
00:02:30,490 --> 00:02:31,960
all the entities.

39
00:02:32,110 --> 00:02:35,650
When you are defining your entity, you have a lot of flexibility.

40
00:02:35,770 --> 00:02:37,810
Let's say the name of the table.

41
00:02:37,810 --> 00:02:42,820
Let's say I would want to map two is not to do, but let's say I would want to map to something else.

42
00:02:43,330 --> 00:02:48,460
So I would say name is equal to to do ABC and save.

43
00:02:52,110 --> 00:02:53,730
Let's refresh this.

44
00:02:53,910 --> 00:02:54,740
Continue.

45
00:02:54,900 --> 00:02:55,650
Connect.

46
00:02:56,560 --> 00:02:59,950
You can see that the name of the table is not to do ABC.

47
00:03:00,190 --> 00:03:05,040
So we are mapping to a table called to do ABC, to do ABC.

48
00:03:05,050 --> 00:03:07,380
By default it will use the class name itself.

49
00:03:07,390 --> 00:03:08,320
I'll remove this.

50
00:03:08,590 --> 00:03:12,590
You also have flexibility in terms of customizing the column names as well.

51
00:03:12,610 --> 00:03:16,630
Instead of calling this user name, let's say I would want to give it a different name.

52
00:03:16,930 --> 00:03:21,040
I can say column and give it a name.

53
00:03:21,880 --> 00:03:23,770
The name is equal to a name.

54
00:03:24,070 --> 00:03:31,360
Right now, if you look at this, it's called user name, but if I save this and come back.

55
00:03:32,240 --> 00:03:35,180
To which to cancel, refresh it and connect it back.

56
00:03:36,150 --> 00:03:39,630
You would see that the name is enough name.

57
00:03:40,920 --> 00:03:43,590
So this field is now mapped to this.

58
00:03:44,400 --> 00:03:47,670
JP also supports mapping complex relationships.

59
00:03:47,670 --> 00:03:48,540
One, two, 112.

60
00:03:48,540 --> 00:03:51,540
Many, many to one, many too many and things like that.

61
00:03:51,570 --> 00:03:54,450
For now, let's remove the column so I'll remove this column.

62
00:03:54,450 --> 00:03:55,620
Name is equal to name.

63
00:03:56,010 --> 00:03:59,130
I would want to continue using the default values which are generated.

64
00:03:59,130 --> 00:04:03,270
Make sure that the entity name is also true and save it out.

65
00:04:04,020 --> 00:04:08,220
And when I actually refresh this, go back and say connect.

66
00:04:08,850 --> 00:04:16,680
I should see to do table in here with the usual column names id description done targeted user name.

67
00:04:16,680 --> 00:04:18,029
So these look good.

68
00:04:18,810 --> 00:04:22,770
One thing you would have observed is target date is target underscore date.

69
00:04:22,920 --> 00:04:26,670
The reason is because there is a init cap in here.

70
00:04:26,670 --> 00:04:28,140
So this is in caps.

71
00:04:28,140 --> 00:04:34,320
So what happens is in a database typically we separate words using underscore.

72
00:04:34,710 --> 00:04:36,420
We capitalized D in here.

73
00:04:36,420 --> 00:04:41,040
That's why the column name is created as target underscore date.

74
00:04:46,220 --> 00:04:49,310
Let's organize, import and save this.

75
00:04:51,310 --> 00:04:54,610
Now, I would want to populate some data to start off.

76
00:04:54,760 --> 00:04:57,790
So how can I populate some data to start off?

77
00:04:59,740 --> 00:05:02,920
I can create a file called Data dot SQL.

78
00:05:03,110 --> 00:05:04,840
So Search Main Resources.

79
00:05:04,870 --> 00:05:07,750
New SQL file.

80
00:05:09,140 --> 00:05:11,330
Data dot SQL.

81
00:05:12,320 --> 00:05:18,470
In the data dot SQL, I can create queries and these queries will be executed at startup.

82
00:05:19,530 --> 00:05:22,620
So I'll say insert into tattoos.

83
00:05:26,230 --> 00:05:29,890
Actually insert into To-Do values.

84
00:05:30,190 --> 00:05:32,140
And you can specify the values in here.

85
00:05:35,690 --> 00:05:37,790
The default generated values start with one.

86
00:05:37,790 --> 00:05:40,190
So I'll use 10,001 as the ID.

87
00:05:44,120 --> 00:05:45,830
The second one is description.

88
00:05:46,130 --> 00:05:50,150
I'll say learn or you can say get.

89
00:05:51,490 --> 00:05:53,830
A certified.

90
00:05:56,620 --> 00:05:57,340
And.

91
00:06:02,940 --> 00:06:04,140
The next one is done.

92
00:06:04,140 --> 00:06:04,860
Which is.

93
00:06:08,230 --> 00:06:09,130
False.

94
00:06:09,160 --> 00:06:11,080
The order of these does not look good.

95
00:06:11,080 --> 00:06:17,770
So what I do is I would define my own columns in here, so I would want to insert in the right order.

96
00:06:17,770 --> 00:06:20,920
So I would want to define my own thing in here.

97
00:06:21,040 --> 00:06:27,850
So I would say ID first ID, then description, then I would want to insert.

98
00:06:28,240 --> 00:06:30,850
Actually, before description, we need to have the username.

99
00:06:33,480 --> 00:06:35,520
ID username description.

100
00:06:35,760 --> 00:06:40,870
After that would be the target date and the last one is done.

101
00:06:40,890 --> 00:06:43,020
So let's insert values in the same way.

102
00:06:43,320 --> 00:06:52,950
So let's maximize this and 1001 user username is in 28 minutes or ranga you can insert for any of those

103
00:06:52,950 --> 00:06:57,750
users and use that specific user get certified the target date.

104
00:06:58,560 --> 00:07:02,100
Let's use a hedge to function called current date.

105
00:07:02,930 --> 00:07:04,910
And is it done?

106
00:07:05,760 --> 00:07:07,140
It's false.

107
00:07:08,420 --> 00:07:09,680
And say this.

108
00:07:13,040 --> 00:07:16,670
If you go to console right enough, you'd see an error.

109
00:07:17,030 --> 00:07:26,030
It's saying table two do not found by default Data dot SQL is executed before the entities are processed.

110
00:07:26,570 --> 00:07:29,360
Tables are created when the entities are processed.

111
00:07:30,510 --> 00:07:32,790
So data dot SQL is executed before.

112
00:07:34,550 --> 00:07:37,820
The table is created and we would want to change that.

113
00:07:37,850 --> 00:07:39,050
How do we change that?

114
00:07:39,260 --> 00:07:41,750
That's where we can make a change in the application.

115
00:07:41,760 --> 00:07:42,740
What properties?

116
00:07:44,840 --> 00:07:47,480
That's something we can configure in application of properties.

117
00:07:47,480 --> 00:07:49,490
So let's open up application of properties.

118
00:07:50,280 --> 00:07:57,420
And type in spring dot JPA dot defer hyphen data source.

119
00:07:59,120 --> 00:08:06,620
Any shirt light zation, make sure that you get the spelling right it springboard JPA defer hyphen data

120
00:08:06,620 --> 00:08:12,560
source hyphen initialization is equal to true and say save.

121
00:08:13,660 --> 00:08:18,040
When I stopped and started the application again, I saw an syntax exception.

122
00:08:18,070 --> 00:08:20,740
It says column in 28 minutes not found.

123
00:08:21,250 --> 00:08:26,980
The reason why it's throwing error is because in data or SQL you should use single quotes.

124
00:08:27,340 --> 00:08:30,340
So let me replace all this with single quotes.

125
00:08:30,640 --> 00:08:33,309
So instead of double quotes, let's use single quotes.

126
00:08:40,280 --> 00:08:41,240
And take this.

127
00:08:45,410 --> 00:08:46,920
Let's see it in action.

128
00:08:48,020 --> 00:08:49,610
Let's start it again.

129
00:08:50,390 --> 00:08:51,230
Enough.

130
00:08:51,410 --> 00:08:52,940
Everything looks hunky dory.

131
00:08:52,970 --> 00:08:54,560
The application has started up fine.

132
00:08:55,730 --> 00:09:02,240
Let's go to page two console refresh and connect again to the table.

133
00:09:02,840 --> 00:09:03,920
Do you have the data?

134
00:09:04,700 --> 00:09:06,890
Yep, We have the data in here.

135
00:09:08,250 --> 00:09:11,850
Let's add a few more tattoos to data skull.

136
00:09:14,880 --> 00:09:16,050
Control a.

137
00:09:17,490 --> 00:09:20,700
So let's add a few more ID should be increased.

138
00:09:20,700 --> 00:09:22,110
You cannot have duplicate IDs.

139
00:09:22,110 --> 00:09:22,800
So 1000.

140
00:09:22,800 --> 00:09:24,090
Three 1004.

141
00:09:26,130 --> 00:09:30,810
I led all the tutus for in 28 minutes itself will be make use of intimidating its ID.

142
00:09:30,840 --> 00:09:33,330
If you want, you can make use of the wrong idea as well.

143
00:09:33,570 --> 00:09:41,400
So let's say get your certified GCP certified and let's say learn full stack or DevOps, whatever you

144
00:09:41,460 --> 00:09:43,560
want to do or learn to dance.

145
00:09:43,770 --> 00:09:45,210
All right, so what do you want to do?

146
00:09:45,240 --> 00:09:50,730
You can put them in here, make sure that the IDs are unique and save them down.

147
00:09:52,510 --> 00:09:59,320
And go back and refresh connect and to do run.

148
00:10:00,960 --> 00:10:06,400
One thing you need to remember is that even H2 is an in-memory database when you restart the application.

149
00:10:06,420 --> 00:10:08,700
All the data in H2 will be lost.

150
00:10:09,330 --> 00:10:11,610
In this step, we got the H2 database ready.

151
00:10:11,610 --> 00:10:17,940
We have the table ready, we have the data ready, and now we would want to display these to DOS connecting

152
00:10:17,940 --> 00:10:19,350
to the H2 database.

153
00:10:19,350 --> 00:10:25,890
We want to use JPA, connect to the database and show the to dos in here and we would want to use JPA

154
00:10:25,890 --> 00:10:30,960
to perform all the operations as well, delete, update, add and a wide variety of them.

155
00:10:31,080 --> 00:10:34,050
Let's get started with that starting the next step.


