#18 Spring Data JPA and H2 Setup

πŸš€ Add to Chrome – It’s Free - YouTube Summarizer

Category: N/A

Building WordCloud ...

Summary

No summary available.

Transcript

00:00

so now let's Implement spring data jpa in this project so the ultimate aim is to basically store this data in the database and also fetch from it now if you can see we do have a controller for all the different uh requests we got get

00:15

uh Post Port and delete basically uh the request goes to service and then from service we have done basically we have done the hard coding of the values and that's what we are getting right now we want to work with database so what are the things we need the first thing we need is a database so basically if you

00:32

see we have talked about different layers we want to work with database so we have to choose a database now and we are going for hed2 as I mentioned before uh next to connect database with our spring application we need uh the connectors as well but once you get uh so yeah we need a connector as well so

00:49

we'll get that so we need a connector between these two uh so that's that is basically your ed2 driver so we need this also we have to create a layer positive layer which will make it work so again let's try to do that in this video or we'll divide into two parts

01:05

let's see how it goes so the first thing is you need to get ed2 and the driver also the spring data jpa capability now how you're going to add that see if you want to add a basic feature in your project or uh any external libraries you have to do that with the help of pom.xml

01:20

file so if you open this what we got till now is we got web and that's it that's the main thing we have of course we have Dev tool as well but that is optional apart from this web we need to get the data jpa and edge2 now when you

01:36

create this project for the first time you know if you remember how we get created this project so I will open the Chrome and we'll go to start. spring.io uh remember when we created this project we have to we have to specify some values here and then we have to add a dependency if you don't do

01:53

that let's see what dependency you get by default so I will click on explore and I will increase the size a bit so this is okay this is griddle I want the mavin so I will say explode okay now you can see by default you only get the spring boot stter not even web so for

02:10

that you have to add web so what you do is you go to add dependency and you say spring web that's the first thing you add and once you add spring web you will get spring starter web so you get this but what if you want to work with jpa in that case you click here and search for

02:27

jpa so you get an option of spring d jpa you can click on this and now you got two so I will click on explore and if you go down uh yeah so we got spring data JP this or something we need apart from this I also need H2 so we got H2

02:44

database driver click on this and it will give you this driver if you click on explore U not just driver it will also give you a runtime so basically it will give you the H2 database see H2 is a in database right so if you work with some other databases let's say say post MySQL

03:02

you need to basically install them on your machine then do some configuration it's a good thing because in the real life project we do that but since we are into learning phase H2 works for us it's a in database and you don't have to do a lot of setup you just add the dependency

03:17

and your work is done it will give you some default settings just use it and make it work okay so we don't have this things in our project right so one thing you can do you can again go to start. spring.io and copy this dependency so you can copy this and you can copy this

03:33

uh that's one way or instead of going for start. spring.io we can search for Maven repository and here you can search for it so I'm searching for spring data jpa I will click on this that's the first one we got and then we have to choose a version so which version to choose here now you have to choose a

03:49

version which you are already using in your project for web so I will go back to my project and in fact you don't have to mention the version because we have mentioned the version here as as a pent so you can it will simply use this so you can mention the uh driver or you can

04:05

mention the dependency without the version so I can pick up two 3.2.6 and copy this looks like this is not the one we are looking for okay that's that's one of the confusion we have uh if you see we have two options spring. a jpa and spring boots start a

04:20

jpa now since we are working with spring boot project we have to use this not that one and again I will select 3.2.6 uh yeah this looks good so I was looking for this boot word uh this is important so I will just copy this go back to the project and paste it here and of course I can get rid of this

04:37

particular version number and reload the mavin changes okay this is done the next thing you need is the hed2 so I will just go back here and you have to get for H2 uh again the same website I'll search for H2 click

04:52

here and which version so you can see there are lot of verities you can go with this one or maybe latest one also looks good double click on this uh I just need to verify if I'm getting the right one so yeah so just com. hed2 database hed2 artifact okay we get

05:10

this and paste it here so these are two things we need so I will just remove this and now by doing this of course you have to reload the maven changes by doing this you are basically making sure that in your project now you have data jpa and also you got H2 how do you

05:26

verify this what I will do is I will start the project to see if certain things are working out otherwise we have to make some changes okay so we got the eror it says uh failed because we have not specified the URL see whenever you

05:41

add any driver we have to make sure not just driver whenever you add spring data jpa basically it will ask you for the URL for the uh database see whenever you con database we have to specify certain things uh it's not just for H2 any dbms you have to mention the jdbc steps where

05:58

you have to specify the connection you so that's something it is it says it is missing so where should we put the URL so that should be done in the application properties so let's go down so in order to do that you have to say spring. data source.

URL and you can

06:14

mention the URL here so what's the URL so this jdpc colon then you have to mention the database name if it is myql semis SQL post post but here we are saying H2 then in H2 we have to mention it is a inmemory database M uh we can also get a full storage it is also

06:30

option permanent storage option is there but you're going for in memory so we say ma'am and then whatever databas name is so I will say test DP or maybe I can say telescope that's our choice we can mention whatever database name we have so that's done right now by doing this let's start the application because that's what it is it was looking for and

06:46

I hope it will work okay we got another error it's not able to find the driver uh should I remove this version I remove the version let's also mention the driver name I thought it would pick up let's type it so we'll say

07:02

spring. data source.

driver class name certain things you have to mention for the properties so it's org. H2 do driver I hope this will work now so basically we do this in the DC as well so you have to mention uh the URL you have to

07:18

mention the driver name also username password but I want to go for default one if it is not accepting we'll type it we don't have a choice so let's restart the application so let's learn from the errors okay moreas okay looks like I made a mistake

07:33

while mentioning the driver name it looks like it's not o it should be okay just com dot it is there cannot load driver let me check the dependency once

07:51

okay looks like I've got it it should be run time because if you see when we were trying to do this it says run time in the dependency I got it test so I forgot to change that the scope let's see if this works now after making those

08:06

changes um so basically you have to make sure that your H2 is there available in the run time okay looks like there's no error yeah there's no error and the tomit is running so yeah so sometime it's good to get errors you learn certain things there and now what we

08:21

have is we have the hed2 database ready we don't have data in there yet but the datab is there but how do I access that database so since we we are not in installed in the system one way to access it is using the URL so I say Local Host colon 8 8090 because that's a

08:37

port we using SL H2 console so to access the database we have to say H2 console enter it will give you this page and you can see there are certain things which are mentioned here so we have the driver name which is this the URL we have the

08:52

database name as teliscope so I will use that uh the username is by default essay I've not spef ify that in the properties the password is blank test connection it says successful I will click on connect and that's our database nothing is there

09:08

because we have not done anything yet but at least we have the jpa uh connected and we got hed2 connected and now it's time to create that layer the repository layer but let's do that in the next video so let's create a reposit layer and make it work