π Add to Chrome β Itβs Free - YouTube Summarizer
Category: N/A
No summary available.
00:00
so now we have our hed2 ready you can see we have the we have the entire console here but if you see we don't have the tables which we want so it got some tables but not something which we want so if you expand this nothing is there for us what table I'm talking
00:15
about if you remember the theory we have talked about the table because we have a class called Product and I want a table which will have this data so the table name should be product the column should be the variables of the product and and the data will be of course rows so how
00:32
do I get that how do we create this table and for this we have to do some changes in the code the first thing you will do is we'll make the changes in the uh basically we'll create a repository layer see service is not responsible to work with data it is responsibility of the repository so what I will do is I
00:49
will create another package here where you will have all the repositories so I will just right click here and say new uh package and we'll call this as repository or you can also say repo that works now in this package you will have
01:05
all your classes interfaces for the repository now the question arise what kind of file we are going to create is it a class or the interface see logically it should be class right because we have to write a lot of code uh to work with database for filing the query and having different methods for
01:22
different actions but what if I say we can do that with the help of interface and you'll be saying okay we have talked about that in the in the theory uh spring. jpa will help you in that uh definitions and yes so what you can do is uh you can simply create the
01:38
interface here I would say class but we'll select interface here and we'll name this as product repo okay now simple interface nothing fancy now in this interface we are going to define the methods oh we can't Define methods in the interface we can using default
01:53
but we have this interface and we can declare methods But ultimately it has to be defined somewhere but jpa will take care of it or sping jpa will take care of it we don't have to worry about those definitions but what should be the method names let's say I'm not even
02:09
mentioning the method names let's keep it blank but one thing I will do I will just say add repository here remember when we talked about uh controller we have on top of that we have ADD controller on service we have add service repository layer will have at
02:24
repository it gives some extra features to it but since I want Sprint at a jpa to take care of everything what I will do is I will extend a class called jpa repository in which you will mention the primary key so basically you have to mention two things uh first the class name which we are working with and
02:39
second the primary key type which is the integer so just these two things and our job is done okay what are the methods let's skip it let's say if you don't enter it let's see what happens now that's one thing we have done with the repository next in the service I'm going to use this repository so I will say
02:55
come back here and we got the product repo let's create the reper as a reference variable and let's say Auto so that we'll get this object first of all we don't have a class right so spring is responsible to create the object of this class or this
03:11
interface so basically someone has to create the class of it and then spring will create the object of it and who is that someone who create a class for it it is your spring data jpa I'll go back here and now since I don't want this I will just commend this part we don't we we don't need those things and when you
03:27
say get products who is responsible to give the products let's see I would say repo dot the moment I say dot can you see that we have so many methods now mind you we are using that repo variable which is the type of product repo and in this interface we don't have any method
03:44
s still when you say dot it is giving you a lot of options question arise from where you're getting all these methods if you go back to repo we are extending with jpa repository now this particular interface has all the methods which are which we are getting there
04:00
right uh if not in this if you see we have also it also extends least uh repository this has some more methods uh if you go to C repository this has some more methods if you go to repository this has nothing but yeah but you got the point right so we we getting all
04:17
those methods because of this JP repository which you have mentioned here so let's go back here and let's see which method will help us so find all looks like a method which will give the Leist of products and that's what we want if you can see I say find all okay next uh we need to replace this and as I
04:34
mentioned before right we don't need to write this all this lengthy code once we start using jpa uh we want a product based on a ID so I will say repo do find uh all okay not all I want one so I will say find by ID and pass the ID here
04:53
but it is giving you an error it is it it returns an optional so I will say all else return me a blank object that works for me next we have to add a product so of course we don't have that now so again I will use the repo to add the
05:09
product now which method we are going to use so if you can see if you scroll down some method should be there which looks good yeah save looks good right so I will use Save and pass the object no errors we're happy update lengthy code right let's remove everything repo dot
05:24
now the thing is we don't have any method called update if you see if I click on if you type update there's nothing the only method you can use is save for both for saving as well as updating we use save so what this will do is it will check if the data is there if the data is there it will update if it is not that it will create as simple as that
05:41
delete no leny code just remove this and we can say repo do delete delete by ID because we have to pass an ID so I'll say PR ID and our job is done it's so simple right look at the code here in the service we have we don't have any
05:56
hardcoded values everything is coming from the database looks good okay uh is it done yet let's try to run this and let's see what erors you get run this and okay what do you think we will get the error or not oh we got the error
06:11
let's see what the error is it says not a managed type which one product is it if you go back to the product this is a component but still it says not managed why because if you see
06:27
the error is because of the hypernet see under under the hood it is implemented with the help of hypernet it follows djpa standards so if you want to have a class of which you want to create a table we have to use one more annotation called entity okay so this is
06:44
The annotation we have to use now it should be happy with it but I think we'll get another error let's see and we got it this time it says okay not the same ER you can see it says entity product looks good has no identify oh
07:00
that's weird see the problem is every table need to have a primary key in this case we know it should be product ID but we have not specified it so you have to mention this as ID so you have to mention hey this is a primary key okay so prod is a primary key Pro ID let's rerun this I hope there's no problem
07:16
let's see let's see let's see okay no problem if you scroll here Tom Cat started to test it I will just refresh this and say connect once again okay but if this time if you say we got a product
07:32
table this is what we want right let's run the query so I will say select uh star from product and as of now it is empty we want to add some data how will we do that of course we can run the insert query here or we can go back
07:49
to our Postman remember Postman yeah it's here so first of all let's get all the products and let's see what we get so we got empty because we don't have any products table it is empty let's add some data so I will say post uh we do have a body here which is this I will
08:06
say I will start with 101 now send okay it says okay there should be one product now how do we test it let's go back to console run hey it's working we got database connectivity it is getting stored in database let's store one more I will say two this time I will store uh
08:23
a water bottle and let's say 200 click on send it works let's try to fetch all the data and this data is coming from database as you can see let's also verify in database and it's working but hold on we have not implemented any of
08:39
these methods where is the query how you getting all these queries if you can see this is empty and we have not even created a class and that's the magic of Sprint at a jpa uh which is creating a class for you behind the scene but maybe you want to see the queries here we are
08:54
getting the products but what qu it is firing behind the scene if you want to know that you can add one more property in the application properties so we can say spring dot data source. show SQL equal to true
09:09
I hope this is a property name if I'm not wrong let's relaunch it the problem is every time you relaunch you will lose the data because you're using in database let's see I will say get we have not got any data and we have not
09:24
even got the query maybe the property name is different is it spring jpa do show SQL okay yeah it is show SQL if you can see U it is actually creating a table for us right so this is the query I'm talking
09:40
about so for every query now so even if you say let's say if you want to say get all the products and see the query here it says select query what if you want to add a product so I'll click on send we have added the product and look at the query it is insert query so that's how
09:56
basically it is working behind the scene we can see the query as well but if you want to change the username password of your H2 we can do that so I will say spring. dat source.
username and let's say the username is naven and spring for the
10:12
password I will say spring. datasource do password I will say the password is teliscope now after making those changes just restart just to secure your H2 console okay it started and if I want to access the H2 console now if I use a
10:28
default username password if I click on test connection it failed so we have to use naven and Tesco as a password connect it worked so that's how basically we can change the username password and we can do all the setup for H2 so I hope this makes sense uh there
10:46
are certain more things in a in the spring jpa maybe if you want to customize this methods uh we got default methods but what if you want to have some customized methods uh let's try to achieve that once we do the project itself you know we can do those changes so yeah that's it from this video I hope
11:02
you enjoyed spring jpa see you in the next video bye-bye