#17 Spring Data JPA

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

Category: N/A

Building WordCloud ...

Summary

No summary available.

Transcript

00:00

welcome back aliens my name is ready and let's continue the series on Spring framework now in this video we'll focus on Spring data jpa so why do we need it see let's go back to the project which we have which we are doing and we have done a lot of things here and just to

00:17

reiterate basically we have uh we are building this application this is a general architecture where you have a client in this case we are using a client as Postman so that's our client right now we can either use a mobile application

00:32

to interact with the server we can either use a client like your uh react application or we can use a API tool called Postman or any other tool using which you can send data to the server and you can get the response and we have done that right the request goes to the

00:47

front controller then from that it goes to the controllers and we do have our controller so if you see we have a prodct controller which has multiple mapping happening and then this controller if you want to do some work any logical work that will be forwarded the request will go to the service now

01:05

it is the job of a service to process any data or if you want to calculate something if you want to do some processing it everything will be done in the service layer and service layer is expert in this things so when I say expert basically you have to code it right so that's the job of a service

01:21

layer and then what if you want to work with database now at this point we have not worked with database you can see the data which we have we have basically half hardcoded it in the service layer not a good idea don't do that we want to store this data in the database right so

01:37

of course we have to use some database it can be H2 it can be post it can be M your choice doesn't matter we can even switch this dbms when we want to do that but let's say I want to work with H2

01:52

here right so when I have all these layers I mean when I when I have a service layer controller layer they're doing their job and now I want to talk to the database now that's where we have to work with the repository layer this R here is your repository layer now the

02:09

job of repository is to connect with the DAT database okay so let's say if a client says hey you know I want all the products so request goes to the controller controller says Okay I want all the products it will ask this service now till this point we are hardcoding it right but now what I want

02:24

is I want this data to be coming from database and to do that we have to do the coding in the repository layer so this is where we have to do all the coding now if you go back to the old days the way you work with repository layer is this basically we have to use

02:39

something called J DBC which stands for Java database connectivity Now using this jdbc we can basically connect the Java code with the help of database with with the database right and to do that we have to write seven steps now it goes

02:56

in multiple ways basically you have to uh load the driver we have to connect to the database or dbms then you have to basically create a query because when you talk about dbms it we're talking about databases here we have to use

03:11

something called SQL so we have to create the query we have to execute the query then it will give you some results which you have to process and then that's how you basically work right you have to follow seven steps and all this code I know we have to write lendy code this will be done in the repository

03:28

layer so basically this is where you have to do all those things now things got bit simpler with the help of uh spring jdbc now there's something called spring jdbc inside spring framework you can use this and it will help you to uh reduce the number of lines you have it

03:43

will make it more standard to reduce the bugs and it's good so instead of using normal jdbc we can use spring jdbc it will help you with the uh jdbc template it's good but what if you want to make it more easy see ultimately if you want to just send data from your uh service

03:59

layer to database layer because what we are doing in this application is just mostly crud right so if you just want to use crud which is create read update delete uh we can use some easy way to do that and that something can be done with om so there's a concept of omm which stands for object relational mapping

04:17

okay so what we are doing is we have objects so in the Java world everything is object right so if you talk about a laptop if you talk about anything anything you want to represent even humans in the Java world or objects bad thing but yeah so everything in the Java

04:32

world is object because it's an objectoriented programming so if I want to represent this remote that's a object if I want to represent maybe this bottle that's an object right everything is an object and the way you create object is with the help of class so first thing you do is you create a class which is a

04:48

BL blueprint and then from that class you create the object if that makes sense now this object will have two things the object will have the data uh so the object will have the data and the object will have the behavior with the help of

05:04

methods so in this case we don't want to talk about methods let's focus only on the data now when you say data what data we have so basically we have U example let's talk let's talk about this product here now if you go back to any particular product which we have it here a product will have the product ID a

05:20

product will have a product name it will have the price right this is a data I'm talking about so every object will have a data so in this case we got uh product ID we product name and record price right so we got these three things this is the data I'm talking about this is the Java world in the database world

05:38

what we do is we have a table right we have a table structure now this table will have a name so table name uh then this table will have the number of columns and they will have a column names right and then they have data as well so each data will be one row so

05:54

this is the rdbms world this is the object world now what we want to do is we want to basically connect them we want to map them that is object so this is object these are called relations and we have to basically map them and if you can do that that's om now how do we map

06:10

it it's very very simple right when we talk about object object has data as I mentioned before so just an example the object will have data so example we got the ID I will say p ID and the ID we have is 101 then let's say we have a name for the product we say p name which

06:27

is iPhone in this case and then we got a price and the price is 50,000 so if you can see this data is stored in that particular object right here is the object I want to store this data in the table of course you can

06:43

write the queries if you are familiar with SQL uh we have something called insert query so you say insert into product table where not where but values are this 101 iPhone 5,000 you can do that but what if you don't want to write

06:58

the queries I mean think think about this you just have to learn Java you don't need to learn any other other language I'm talking about SQL here so can we do that so yes that's where we can use om who simply says hey you know give me the object it's my responsibility to store that in database

07:14

so it will basically convert your or not convert but translate your object into SQL query and then you can save that in database so your object data here becomes one row so one1 goes here the P name iPhone goes here and your prize

07:30

goes here so that's your one row if you have multiple objects you will get multiple rows but the question Still Remains who is responsible to create the table who is responsible to specify the number of columns who is responsible to name the columns are we going to do it what if I say you don't have to do it

07:46

someone else will take care of it and who is that someone else of course the orm tool so basically we have to use some tool to make it work now that tool is it a magician to know everything see the thing is the table name can be we we can get the table name from the class

08:01

name right we have a class name product so we if you can see we do have a class called Product right so we can get the class name as your table name what about the columns each variable becomes a column we got the column as product ID we got the column as product name we got

08:18

the column as price so the whatever variables you have you make them the T the column names and what about the rows each object is one row so if you have 10 objects of 10 products you will get 10 rows that's how basically you do the mapping and who is responsible to do all

08:35

those things for you is the orm tool examples we got hibernate is a very famous one uh we got Eclipse link uh we also got my Bettis my bet is not fully urm but yeah you can use it but the most

08:50

famous one is hibernate it's full-fledged uh om tool and you will find everything there so this is a good one good tool to use so we got hbid but if you see the title of the video it talks about jpa not hibernate right so why see the thing is hibernate is a good

09:06

tool but let's say in future you want to move from hibernate to some other tool you basically have to do a lot of changes to your code is because all this tools don't follow the same standard or maybe they are but how they following the same standard so what we got is we

09:22

got something called jpa which stands for Java persistence API so jpa are just standard and hibernate basically implements those standards Eclipse links basically implements those standards right so all the most of the orm tools Implement jpa

09:38

standards so in future if you want to move from hibernate to Eclipse link it will be easier imagine if you do if you know how to drive one car it is easier to drive other car is because they follow the same standards right that's why we have something called jpa now in the world of spring now spring since is

09:55

a big project it's got a lot of modules inside it they got a special special tool or special module called Spring data now when you talk about spring data it deals with data but we want to specifically mention I want to use jpa so the module name becomes spring data

10:11

jpa and when we use it the storing of the data in the database becomes very very easy okay you will see that in the upcoming videos but I just wanted to give you the work through what are we going to do and why jpa is so important so with the help of spin jpa we can

10:28

store this data which we are are doing in this service layer in the database which D which database we are going to use we are going to use H2 which is uh in database easy to set up easy to use and for learning purpose it's a good tool so we are going to use H2 and we are going to use SP jpa to

10:44

store this data in the database and to fetch data it will be fun and easy to work with and yeah that's it from this video where we talked about the theory of spring data jpa how do we Implement that let's see that in the next video bye-bye