Spring Data JPA tutorial - Unboxing Spring Data JPA Magic | Spring Boot | JPA | MySQL | Hibernate

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome in this video we're going to be learning about spring data JBL you know that when it comes to springboard springboard does a lot of magic for us and today we're going to be learning those magic tricks how basically springboard does a lot of Auto configuration when it comes to the data jpa this video is the introduction to Spring data jpa where we're going to be diving in and we're going to be exploring the spring data jpa as a framework and will be understanding that how different things are going on in this particular module so let's welcome our hero of the movie today which is spring data gpn let's Dive In foreign [Music] recording in progress so what should I do okay so I have a lot of things in my mind because I wanted to show you a lot of different things today okay because you might you might hear that question from us transaction management how it is working in Spring boot we are not enabling it right so the developers like if you have been working in industry for a few years you're gonna be having a lot of doubts because spring boot you know is going to minimize a lot of different things that is why I have forced you guys to watch some of the lessons on hibernate right but if you have not watched also that's that's fine I am assuming that everybody have at least have a little bit of idea about having it if not you have a long weekend going to learn a little bit right because spring boot will feel like a magic but we have to understand we have it's not like we're just watching the magic we have to feel like okay how that magic is happening right if you know that then you can actually utilize spring boot for the maximum okay so today what we're gonna be doing we're gonna be doing a very simple thing it's like setting off everything by ourselves and then converting the spring project to a spring boot project what I mean by that basically we'll create a spring project okay complete spring project we will will feel like spring boot is not there and will try to understand what this spring data JPM means and what it is doing okay and once we configure everything normally then we're going to be jumping to Spring Boot and gonna be like you know making it automatic okay so that we don't have to do a lot of different things so what I want you guys to do I'm sure that a lot of people are here um yeah maybe I know a lot of you having five plus six plus ten plus years of experience so um yeah for you guys might be it'll be like okay I understand what this guy is doing yeah if you are somebody is having less than two or less than one years of experience okay or I I feel like you know if there is somebody who have taken this course also watching this recording what I want you to do just see what I am doing you don't have to do all these things and let me make you really really give you an assurance that you don't have to do all this stuff by yourself whenever you are building a project but as a Dev I know you guys have joined here because you know your first question to me is like you know bless I want to learn things in depth right I don't know what do you mean by depth and I I really care when you say me that of unless I want to learn things in depth I feel like okay what depth I can teach you but yeah today we're gonna be learning something in depth a little bit it's not depth it's like the basics it's like the understanding that the developers need to have okay so no pressure on you guys just hear it answer me the questions there will be some new classes or new classes new interfaces name you'll be seeing no worries the class name just just see it and ignore them it's not like okay I have to understand all these classes I have to understand all these interfaces I have to understand all the methods which is present no you don't have to do that you know in in your project what you basically do you basically call the step method you'll basically call the update method you'll basically call the delete method and the data will be deleted you call save the data will be saved nothing more than this you will be doing you know that most of the time right so nothing to worry just watch and understand okay this is what happening behind the scene all right so let's start so the goal for the goal for the day okay what it is very simple we'll be creating an application their application will be saving some data to a table okay what is that table same table I have the test zero one database okay inside this I have a student table I have some column student ID student mobile student address and student name we have to save the data here that's it that's the simple requirement but why I'm I'm saying like you know we're going to be doing doing a little bit of internal stuff today because I'm not going to use spring boot we're going to be using normal spring we're going to be using spring we're going to be using jpa we're going to be using spring data jpa and we'll be seeing like you know how it is beneficial okay by doing everything by setting it off everything by ourselves okay so let's just start a Marvin project so create a map and project and let's do a next I will choose this as internal okay map and archetype quick start let me just click next group ID com dot seleniumexpress dot um data jpa demo okay artifact ID is like let's say data jpa intro okay let's do a finish it is building my project just trying to create my map and project it's downloading a lot of different things generating the project why to confirm why and it's going to generate a project for me okay there you go data jpa intro okay now the first thing what I'm gonna do over here I will be creating an application which will save a student object to my student table that's it simple thing I I am going to create an entity here called student and that entity I will be I'll be saving in my student table here okay simple I need to fill the data inside this table that's it right so in jpa you know what we what we basically do we are creating some Java application we'll be creating some Java object and will be storing their Java object to the table that's it simple right so first we'll be creating an entity so I'll just go ahead and click on new I will click on a new package and I'll create a package called com dot selenium Express uh data jpa demo dot intro okay I will just say dot entity okay and here I'll create my student entity right so I'll just create a simple class and this is going to be a student class all right and that's it so I will I will not be typing the entity and all The annotation you know how to do it so what I will be doing I have already copied your code control a control C let's go back to here from here and I'm pasting it okay um that's it so I'm I'm I've copied other student class you can see the table name is students here because in the database you have the table where is the database right here can I move it to okay it is here okay all right so you can see the table name is students so here in my end it is the table name instruments I have a at entity annotation that means this is the Java class which is the representation of this table I'll be creating the object of the student we'll be storing it over there in the table so now I have this ID as the primary key I have the column Associated you can see I have the student name column I have the mobile column associated with the student Mobile in the database and I have all this um you know at column annotations I have the gator and Setters for the same and the two string method simple now the thing is I um I have written all these things but I am getting this problem right I cannot import it control space I am not getting any suggestion so what dependency I have to add I want to hear it from you at entity a table at ID at column at generated value hibernate okay hibernate core we can add hibernate core also have this entity because hibernate is the implementation of jpa so whenever you are creating any application is it good to use jpa or it is good to use hibernate foreign [Music] what about if I don't want to use hibernate right I want to use Eclipse link I want to use my Betis then it's better to code it with jpa and provide hibernate as a implementation or as a vendor think like jpa is the setup interface and hibernate is the implementation to those interface hibernate has the logic jpa has just a method okay so let's just add the jpa dependency to our form.xml okay so JPI dependency in the sense what I have to add anyone so I have for now I have only the jio dependency here let it be here so jpa means what dependence you have to search anyone right starter data JP is a spring dependency right but yeah so jpa comes from the job a jpa basically came from the Java team so what is the dependency name anyone yes yes the persistence API right so previously um we used to use the Java extra persistence and right now it is the name has been changed right so I'll say Jakarta and I will just say persistence API Jakarta persistence API let's search for this check out the persistence API you can see it's the release came in 2022 let me just click on the 3.1.0 version copy that and um I'll be pasting it over here all right and people who are hearing this recording we are recording this on 2023 the month of July 7th July okay 2023 so as of now we have the jacket the ladies officer in the older version just go for the Java Express persistence okay now let me do a control s it'll bring me all the jars and it will download it and we'll keep it inside the map and dependencies so our jacket the persistent stuff came here let me go over here and do a control shift o and yes all those Imports we got and our issue has been fixed perfect so we have uh yes so I've seen the previous uh recording okay we have directly used if I have used hibernate that's not good if I have used hibernate that's not good right maybe mistakely I might have done it uh Abdul so uh you just change it to like you know Java if I if if we are in an older project just change it to Java extra persistence okay but here right now the Java extra persistence has been replaced with jackers the persistence okay perfect now but yeah the hibernate has also the same you know annotations you can also use it from the hibernate okay but better just go for the JP annotations okay now our table has our entity has been ready so in my application what we have to do let me go to my application we have to create a student object here right we have to create a student object like student Village okay is equal to new student then dot set address let me just say blr and a village start Set uh mobile let's say 99999 then let's say a village start set name uh in the name let's say okay now I have this um Avila's object and I have set all these things what happened here create set method oh I haven't saved it Ctrl s okay now I have this object over here right now we have to save it now here comes the thing if I want to save it then what I have to do I have to use how can I save it what I need data source because first my application need to connect to the database right my application need to connect to the database so that I can save this object so first I got one answer from you data source what else I need how can you save this object what method you have to use persist method assistant entitymanager dot persist right okay entity entity manager comes from what jpa or hibernate or jpa so let's just do one thing let me just create a another package first and this is going to be dot uh Dao okay so in my Dao right now this is my Dao let me create a let me go with the standard let me create an interface let's say student Dao okay an inside student Dao let's say we have [Music] um we have one method okay let's say a public void saved student okay and safe student will take a student object and we'll save it so let me import it by hitting Ctrl shift or control s right let me give a impulse for this new class and let me say student Dao impulse okay and now this need to extends to or just need to implements what my implement or my interface so implements uh student Dao all right so now let me just override the unimplemented method so I have to use this method from my app I'll go to my app so I need to create the student object sorry student Dev object so I will just use student Dao let's say student Dao student Dao is equal to new student Dao info and let me just copy this guy and just use student.o.save and just passing this is the last subject that we have created here now let's go to the save student and to the implementation of this here we have to give the logic okay how can I save so I'm getting the student object here that obvious object that I have created from my app.java I am getting this object right here right now so which method I'll be using persist method to save it so for that what I need what I need entity manager and entity manager so NTT manager advocating the option sorry what I hit let me go over there so far I don't have any dependency Mark that thing entity manager I'm getting it from jakarta.persistence so entitymanager entity manager and I will be right now taking this and entitymanager Dot persist and I'll be persisting the student and I will be just saying this out and I'll be saying a record inserted okay cool done right but will it work no entity manager object we will create yes we have to do okay so that means you an all pointed exception because entity manager will not be instantiated who will be creating the entity manager object right so um entity manager object where you wanted to create by Spring and for that we need to use autoware or something over here right and also here one more thing in the app.java we're creating the student div object by ourselves we don't want to do that right we want a container a spring container we want to give the responsibility to Spring to create the student Dow objects right and how to do that by use by create bringing in Spring to our application will tell spring hey Spring uh we will just use a repository annotation here so you will create the object of the student.imple you will be creating the object of entity management dependency what dependency so that's what I wanted to hear so let's just go over here and just go to here bring in the spring context okay so spring context click on this 6.0.10 copy this uh go back and paste it uh and I'll just move a little uh to the right Ctrl s and now we got all the spring core spring context dependency so let's just go to um now let's just do one thing let's just go to our Dao input first of all let's just do a couple of things over here uh let me just maximize it first thing is I'm going to be using it repository annotation the next thing I will Auto add this thing Auto word and okay so now it's good right so we have all the things now we have to get the object for this right we want spring to create the object for this so what we need we need a config file um because we want spring to scan this thing um so what we will do we will go here you understand right we will be needing a config file because it need to be scanned because we want to create a application context object right now here we don't want to create the object by by ourselves like this right we want we want to ask spring about this object like like this let's say we want to create application context object so maybe yeah let me go with The annotation config application context a notation config application context let's say this is my container is equal to new annotation config application context and now to The Container only you're going to be asking right the student da object you won't be creating like this so let me delete it let me say container dot get bin because get me the bean of what student Dao so this is the class name wow what is the bin name what is the bin name student now okay okay let's just go over here and let let's just say student now import and S will be small and you can also add student Dao directly because if this simple object will be available uh it will directly give you because the now implementation is student now impulse right you can also you can directly ask student.imple or you can ask also student doubt if the student.imple is available it will give you that object but let me just make a student now impulse because it will not confuse anyone so okay so I'll just say student Dao impulse dot class okay control one assign statement to a new local variable student now import and let me just say student down here okay and I will just copy this and yeah let's just save it's just saving it right now can I start my container will it work right click run is Java application why it will not work configuration absolutely you can see it is giving me giving me a annotation config has not been refreshed yet so it is saying okay you have created The annotation config class but it has not refreshed because I don't see any uh you know config file over here so I can just do a container dot register and you can just um you know give your configuration class over here and then you can just do container dot refresh okay otherwise you can do it in one line how can you do it you can just provide the configuration class name right here so why do you need the configuration class because this Bean will be searched inside the container and how you want to create the container I want to create the container using a configuration file so let me create another package here another class this is going to be my let's say app config class app config app config and let's just do a finish and here I will just go ahead and make it a configuration [Music] configuration like this and also I want to activate the component scan okay and that's it so component scan I can just scan all the packages starting with com let's say Ctrl s now I can take this class name and I'll give it to my spring my application contacts and ask my container that okay you use this file to create your container now spring container spring will create the application context going to this file this file is uh which one this file is the app config this one right now it will go ahead and scan for all the packages and now whenever it is scanning all the packages it will also go to the Dow package it will find this import class finders repository annotation and depository by default is having add component so it will be eligible for component scanning that object will be going to the spring container and now we can go to our application.java and here we can ask for the bin right we are as we are asking for the Bean here that okay container get me the bean should endow impul I know you have created the object for that because I have activated the component scanning I'm getting the object uh now I'm creating a student object using this object right now I'm calling the substudent method cool Ctrl s Ctrl shift or to remove all the unnecessary Imports right click run is Java application let's see if it is working right now yeah new errors now tell me how to fix it okay see what we are having unsatisfied dependency exception error creating the bean name called student dial import it is not able to create our Bean for student our impulse because there is a unsatisfied dependency expressed through the field called entitymanager No qualifying bin for Jakarta persistence entity manager available it is expecting at least one bin to be available to inject it inside the dependency that we have inside the class called entitymanager so it is not able to create the beam for student Dow impulse is not able to create the object for this because this bean is not able to uh is it is not able to find the beam for entity manager it's not available inside the application context so the auto varying cannot happen the dependency injection cannot happen so how can we create the entity manager object right now this is a question for you yes in the app config which is like we create being over there how can you okay the first question is um okay entity manager in hibernate if you compare entity manager it's like what session session where you can just use session dot save session dot uh session dot find system.get all these things like entitymanager you can just use the methods like persist remove find merge and all these things this is like a API now this is like our session object okay how to create this entity manager who will create me the entity manager entity manager Factory because you cannot create the entity manager directly you need a entity manager Factory for that okay so Ctrl T entd manager Factory okay this is the guy we want to create right Ctrl T here okay uh is is coming from the Jakarta dot persistence okay so how to create the entity manager Factor right now I told you one thing if we are using spring we can use a class to create entity manager Factory so what is that class name I can just write entity a manager Factory manager Factory and before that I can search for local let me just write again local local yeah local container or something entity entity management entity manager Factory win right uh what is that local entity local container or something like that okay let me let me just go to the web and search for let's say local entity manager fact local entity local container entity manager Factory Bean oh this is a spring stuffer you guys are able to hear me my connection is there or is dropped what is keep on searching hello guys can you hear me recording in progress sorry I just got disconnected or something yes yeah oh I'm I'm sorry I don't know why it is happening okay let me just share again and to create the entity manager okay we need the entity manager Factory okay and to create a entity manager Factory uh we need uh some special class right a special class like this local container entity manager Factory beam something like this we have to create okay and you can see this this is this is something that I'm trying to find out because this class will help me to create the entity manager Factory but you can see it is coming from which package spring framework.orm this is a spring specific class and the problem is I have not added the spring support to this data jpa project now the thing is that okay I I will I'll come back to two different things right now okay we need to create the entity manager Factory and for to create the entity manager Factory we need this class local container entity manager Factory the local container is like you know if you are using any container best application like spring go with the local container entity manager Factory bin this is something spring will give you this is a class name you just create the bin for this and one more thing if you remember um somebody told me that okay blast you're creating an application and you are trying to save an object okay you are trying to save an object and if you are trying to save that object to the database you also need a data source and how to create a data source in Spring anybody remember if you're like you told me right uh right over here to the config package yes okay can can anybody remember the package name in the class name how to create a data source in Spring yes please yes exactly so we we need to we need to create two things right now the first thing is that we have a problem here our entity manager is not able to we are not able to instantiate our entity manager that's our student now impulse failing the sanity manager we are not able to find it out we have to build a bin for this second thing if also we want to connect to the database if we want to connect to the database then we need to also we need to have to establish a connection from my from my application so we have to create a DV config file okay here inside the config file let's just do couple of things let's instantiate our entity manager let's build our entity manager Factory and let's just uh create a connection okay first let's just try to create a connection okay let's do set up our connection to the database um so if I want to set up a connection so what should I do I have to create a data source I have to create for to to connect to the database what I need a data source data source means what it holds the connection object so first I need to create a data source to connect to my database so so right now you just told me that I have to create a driver manager data source you can see driver manager that I have which will help me to create a connection but in Spring I should use something called driver manager data source that also I'm not finding because I have to add the spring support to the GPA and for that what I have to use I have to use something called a new dependency called Spring orm so I'll go over here and we'll look for spring orm spring orm go for this uh spring orm go for this yes object relational mapping click on the latest version copy this go back to your application paste it here and Ctrl s and now if I'll go to my DB config let me just go to the DV config and close all the other tab okay okay so here I have to create the connection I have to build a data source and also I have to instantiate my entity manager Factory these two bins I have to create okay for connection I have to use what driver manager data source right because I have to create a data source and you can see right now driver manager data source is present coming from work.spring framework.jdbc.data Source because if you if you see the dependency that you have added right now called Spring orm and if you go inside the spring orm it's also bring in some spring jdbc stuff so this driver manager data data source is present inside the uh the jdbc package right so that's why you can see right here we will be setting off the bin for driver manager data data source here it is driver manager data source this will help me to build a data source or a connection to my database and where it is where it is present link to the editor and see it is present inside the spring jdbc um you know jar okay it inside this data source package of the spring jdbc chat I hope this is making sense okay and the other thing that we are looking for we need to also instantiate our entity manager for entitymanager to instantiate we'll be needing something called local container entity manager Factory bin and it is also coming from org.spring framework.orm.gpa so that's why I've added that orm dependency because this class will help me to set up the entity manager Factory from what the entity manager will be created and I will not get any you know null pointer for that okay so now let's just go ahead and set up this couple of beans so first of all let's just go ahead and set up the connection so I'll go ahead and set up the connection for my database so I'll create a bean called driver manager data source so driver manager data source uh I will say data source okay and let's just create the bin for this let me just maximize this okay and I will create a bin for this this will be the connection to my database and how to how to basically um instantiate the driver manager data source you guys already know right I have to just initialize this class this is the data source class data source is equal to new driver manager data source and here I have to put my connection related information right so data source dot set URL you have to provide my set URL over here data source dot set username and I have to pass in my username over here for my database in data source Dot set password okay so I'll be connecting to a database the database is this one and this is the test01 database so I'll go over here first of all I'll copy the connection to the clipboard and I'll come over here put the connection over here and here my database name is what my database name is test01 here I have the student table where the data will go right so test 0 1 is my database name copy this and paste it over here the username of mine is root and the password is a flash okay that's it and now I will just return the data source object so now I am I'm establishing and connection from my application to the sorry sorry three zerosm to yourself oh three three oh okay okay okay okay yes yes yes yes actually here we are using at the red beans yes that is if you do this annotation then spring creates data source exactly and we'll keep it in the container so the data source in jdbc how you create the connection you remember uh Aditi yo yes yes you you have this class called data source from java.sql you do get connection or something right data source dot uh create connection get connection you pass in three parameters and this is how you create the thing over there so this is a spring data source class um so this will help you to create the connection but but but understand this in production never ever never ever use this even you know right if you go to this they will mention it somewhere that simple implementation of the standard jdbc data source interface configuring a plain old jdbc via Bean properties and returning a new new entry you can see this is not a actual connection pool it does not pull the connection it just serves a simple replacement of a full blown connection pool implementing the same standard interface but creating new connection in every call so basically what it means that spring is warning you that do not use this class in production because in production what you need you need a connection pool where you can when you are starting your application right that time only lot of connection will be created and those connection will be only reused but here what will happen whenever you are setting off the connection to from your application to your database whenever you are let's say in your application yeah it will create connection every time it will say you are you are going here right data impulse here you are doing a persist so whenever you are doing a persist that means what your application need to connect to the database that means what you need a new connection right and that connection will be created fresh right and every time if you are creating Connection in an application it is a very bad thing so in real time what we do we do create connection using connection pool but driver Managed IT yeah driver manager data source is not going to give you any connection pool right so you have to yeah you have to set up the connection pool by yourself but we are doing a simple application right now that's why I am going with the driver manager data source but it is not a yeah it is not advisable to use it in the production we will change it to some other connection pooling we will bring in some connection pulling later but right now I don't mind if I if I'm doing any saving deleting updating and every time if my application is creating a new connection I don't mind because I know it's just a simple application right but yeah driver manager data source right now helping us to connect to the database because we are giving our connection properties over here that's it simple yes go ahead go ahead go ahead go ahead here the account we use it yes so basically what is the condition Yeah so basically uh we will see that uh we'll see two things so here here are Aditi the connection object the driver manager data source object will not be created by Spring if spring is creating this object automatically it is available inside the application context or inside the container you can just directly create the data source object and make it at Autobot right if it is if it gets out of it right now now once you have this object you can go to any of your class and you can just go and do it driver manager data source yeah here you can just do it auto word right now because it is available inside the container right now but from from here you can just do data source.getconnection to get the connection but right now uh that you know that object we have created that's why you will be able to use the it out of it right so we have to create the bin for this so okay so right now it's a simple press application we are creating no spring boot nothing so uh so we need to create the bin for that driver manager pin we have to create okay so it's like everything we are building from fresh okay yes sir all right so like the data source as I said we are also having whenever we are launching up our application if you remember we are also having another problem if I'll go to my app.java where is my app here so here also if I'll go and launching up my application we are getting another problem what is that problem anyone entity manager bin if you see here uh inside the student our impul it is not able to create the object because the entitymanager bean is not available now we have to also create the entity manager beam okay uh entity manager you guys told me that it will be created from The Entity manager Factory in Spring we have one more class which will help us to create the entity manager Factory and that is called local container entity manager Factory Bean okay this is the object you have to create right I'll copy this you can say internally it is using entity manager Factory being if you go over here you can see abstract one is basically um is basically helping you to create Factory of entity manager Factory C this bin only you want to create right you told me if you want to create the entity manager you want to create it from The Entity manager Factory you can just do entitymanager Factory dot create entitymanager and it will help you to create the entity manager right so you can do all your save delete update all the operation from The Entity manager but to create the entity manager you need the entitymanager factory but in Spring the way we create the entity manager Factory not by directly entity manager Factory entity manager Factory is equal to new entity manager Factory you you won't do it you do it like this in Spring how they do it they do it by using this Factory bin you can see there is a classical Factory bean and they have this entity manager Factory within this diamond bracket so Factory bin is a um is a spring specific class if I'm not wrong it's a spring specific yes so it is attack whenever like you want to create any Factory object in Spring like Bean Factory uh session Factory entity manager Factory then you need to use realize this bin this is a special uh you know interface which will help you to create the factory related Bean or object so we're going to be using uh this Factory being this one we came from here if I'll go back we came from ah Here Local and local container man a local container entity manager Factory bin this implements to this class and this class is implementing the factory bin or whatever whatever we don't give a damn about it I don't want to get confused let me just close all this thing and let me go to my uh config file the dbconfig file and here let me just create a entity manager Factory object so as I said in Spring how can I do it I have to use local container entity manager Factory bin okay you can see also there are there are classes called NT LSA star entity manager Factory okay so there there should be a class called local entity manager Factory pin there is another class called local container entity manager Factory bin remember whenever you are using any Standalone application you can go for the local entity manager Factory bin but in Spring is basically a container spring is providing us a container so for container based application we can go for local container entity manager Factory bin okay so let me just create the bin for this to create my entitymanager factory and I can just go over here and I can just right now do entity manager Factory bin I want to create and then I will just return this object right let me maximize it and let me just start creating the object for this so let me create the bean first of all this Bean is for my entity manager Factory and now I will be instantiate this object so I'll instantiate it let's say entity manager Factory is equal to one second guys please be on mute yeah everyone try to be on mute just check if you are not muted yeah so I'm creating the Bean for this okay now I'm creating the entitymanager factor bin to my entity manager Factory bin what should I tell I will tell okay you want to create the entity manager Factory bin okay so this is the connection to my database EMF dot set data source okay entitymanager Factory this is your data source that you want to use this is the data source right copy this call it over here okay to my entity manager Factor bin I'm telling okay what's my data search what else I have to do entity manager okay entity manager is what guys entity manager from The Entity manager Factory you're going to be creating the entity manager okay so your entity manager is basically doing what anyone entity manages yes what is that what is that what is that you are saying something no you just said something entity manager is what [Music] operation but entity manager entity manager fill the world so what it is managing entity entity right entity manager is at all this is the persistence context persistence contextness entity manage the persistence context means only entity manager entity manager means persistence context it puts it is it is basically managing some entity inside that persistence context right so it is managing The Entity so from where it will get the entity so we will be just doing uh packages to scan and here inside the packages to scan we'll be giving our entity package where our entities are there so this is our my entity package where the entities like student is there right so I will go copy this name so in this package only I'll store all my entity the package where you are storing all your entity give that package information to your entity manager Factory okay so right here so whenever you are creating a factory this is where I am keeping all my entities so that's it just return this one okay there you go your entity manager Factory Bean is ready right uh cool so you have yeah so the data source which entities all this table which if this data source only exactly exactly so basically uh so we can give good question very good questions right now see we are creating one entity manager Factory we are creating this entity manager Factory from this data source data source means what your uh your connection information to the database right now I have one database you might have many database right if you have many database you might have different different data source configuration like this Ctrl C again control V here it is test two database from uh let's say Oracle or something right you have your uh username goes here your password goes here and you are creating another entity manager Factory being for that and giving that data source to that so yeah so whenever you are creating any entity manager factory make sure so that's why it is good uh okay I'll show you the this question basically brings me a lot of different points we're going to be talking about that in a little um after a couple of minutes but yeah we have the entity manager being a factory done right now we are just giving the data source and packages to scan over here you are tested okay few more things I'm going to tell you in a minute so I think all good right so now everything is okay so all happy right my student now object right now will be created because the entity manager is available because the entity manager will be created from The Entity manager Factory I have written a daughter where so let's see that if spring is able to create the entity manager for me okay so Ctrl shift o let's just go to the app.java let's just do a right click let's just do run let's say like if my student object is getting saved right now see if it is a new error or a same error understand the exception and just let me know creating a bin name with entitymanager Factory being defined in the class plus blah blah blah no persistence provider specified for the entity manager Factory bin configuration uh and uh choosing persistence unit info does not specify a provider class name either so what do you understand over here it is saying does not specify a provider class name either it is a saying no persistence provider specified what do you understand from here no persistence provider specified what do you understand from here yeah exactly right now it is saying okay how can I function man you have just added GPA okay but for the entity manager what is the implementation you know what I cannot function by myself right I am jpa you are creating a entity manager Factory here right here where is that where is that config file DV config right you are creating an entitymanager Factory here but yeah how can I function right you need to tell me who is the provider is a jpa exit hibernate is it Eclipse link it is my Betis or what and for that you have to add one more property here EMF dot set provider class okay you can see set provider set persistence provider you can add the persistence provider here or you can alternatively also add the vendor information directly you can just use like EMF dot set I think set jpa vendor adapter okay any of these things will be fine okay set jpf vendor adapter and who will be my vendor my vendor will be my vendor will be who hibernate okay let's say new hibernate uh jpa vendor adapter okay so I'm saying okay my vendor will be hibernate okay so I'm specifying my vendor over here let's just do a control s let's just go to after Java let's run this one more time let's see if it is a new error or it's running fine okay another time new error understand the error right now green creation exception error creating the bean name entity Factory bean and it is saying uh dvconfig class failed to instantiate uh local container entity manager Factory being the bin that I have created Factory method this the problem is through an exception work dot hibernate session it is trying to create if you come and scroll down you can see it's failing to instant say this being you can see there is a problem here org dot hibernator session see whatever it is Clash not found what class it is trying to instantiate scroll all the way see it is trying to instantiate a classical session because entity manager behind the scene will create a hibernate session because you are using hibernate right so it will create a session so that you can just do some session related uh stuff here but it is not instantiating session because there is a class not found error and it is saying the session class of hibernate is not found why class not found why why class not functions dependency we have to add however many dependence you have to add correct so let's just go to here and go to the palm.xml and let's just bring hibernet core so go back to here and let's just do hibernate core search for this and go to here go to the hibernate dependency copy this Ctrl C okay go back and paste it here right this is your hibernate core and Ctrl s let's bring in this one and now can I go to my app.java and run this one more time run it run as Java application are we getting any same error or different error now you see the error again let me just give me a minute guys yes so what is the problem right now SQL exception no suitable driver found ation MySQL driver so we are connecting our database uh we are connecting our like in a project to the database which database mySQL database so our driver is not present right so we have to add the MySQL driver here so let's just go over here and let's just search for MySQL connector and disconnect the MySQL driver copy this uh copy the MySQL driver come back paste it over here Ctrl V control let me just resize it a little bit Ctrl s there we go let me go to my app.java class list right now try to persist the student object run is Java application will it work I don't think so problem now you see error unsatisfied dependency exception again now again it is saying student you will not be able to creating uh because the entity manager no qualifying been found for the entity manager so what is the problem happening right now guys it is saying that student Dao whenever I'm asking for the student our impul object the student or impulse object is not getting created because this is not getting out of word but I told that the entity manager will be created from The Entity manager Factory so you can see in the app app.java sorry dvconfig.java right here I told you that whenever you're creating the entitymanager factory you are you are basically um creating the entity manager Factory out of what the entity manager will be created but it is saying the entity manager is right now not present because the bin does not exist for that so what we have to do the entity manager bin is not present so Auto word cannot happen so what we can do over here can we come over here and create a NTT manager bin entity manager being like this entity manager entity manager and uh should we create a bean like what is that entity manager what is the entity manager Factory right so we can right now Auto where the NTT manager Factory entity manager Factory and do should I do like entitymanager Factory dot create entitymanager and we have to it will give us the entity manager right now uh and we have to return this entity manager and we have to create a bin do you think I have to do this okay if you think this if you think I have to create a bean like this uh right now I have entity manager Bean right so do you think the problem will go away no no yesterday exactly we don't have to create this entity manager being like this by ourselves right now for your satisfaction I have created The Entity manager Bean right how to create the entity manager bin from The Entity entity manager Factory only right we have already created The Entity manager Factory so this will be Auto web so no worry so this will be autoware so Auto word okay this will be Auto World right because the object I have already created this guy will create me the entity manager Factory it is already there in my container I'm Auto wearing it packing it creating entity manager over here and returning it so now see like if this Bean will be injected over here I I feel still you're gonna be getting the same exception right click run is Java application and you will be seeing you're gonna be getting the same it is saying record inserted but it like the entity manager Bean is present right now okay because the I have created The Entity manager manually over here it is saying record inserted but I am sure that you will not get any record over here see you are not getting record because now here comes two things now everyone please just get a little bit just get get serious right now because I'm gonna be telling you couple of different things and from now on the drama start look at that right now the first thing is this bin you don't have to create you don't have to create the entity manager pin by yourself okay if I if you're not create then that auto wearing will not happen right this Auto wearing will not happen and whenever you will go to app.java if you're going to be running this this will fail it'll tell okay the bin doesn't present the bin is not present so you will not be able to auto air so uh on no such bin definition will come for entitymanager Okay cool so remember one thing you cannot you cannot you cannot Auto add an entity manager how true is that line yesterday I have done the auto wearing whenever we are doing some spring boot project right you can Auto you can do auto wearing but there is some special condition will be coming back to that we can do that in Spring data JPM we have not introduced spring data as API yet so you cannot do auto wearing in entity manager no no and auto wearing you cannot do right now as of now whatever we have done Vijay Auto wearing will not work okay but I told you that we have already created um the object for entity manager Factory right in our DB config here this guy okay now what I want I want to inject a entity manager object to this guy okay entity manager is a spring thing or a Java thing entity manager is a spring stuff or a Java stuff guys Java stuff right is it is it is there since 2006 if I'm not wrong and to instance c n one one more one more thing guys like yourself like your session entity manager is also not thread safe okay to make it thread safe and just forget about the threads thread step stuff just understand this much if you want to in initialize or inject The Entity manager you have to use a special annotation entity manager is what entity manager is what is a persistence context I told you right just use The annotation here called persistence context this from where it is coming persistence from the jpa right now I'm using this annotation okay and I feel right now the dependency thing will not come you can also do auto wearing but I'll show you when okay but now I'm just using this annotation Ctrl s okay now do you go for a little work persistence context you know the entity is a entity manager itself called a persistence context persistence context is a place which basically contain different entities right it's basically takes the responsibility of persisting of entity or doing different stuff it's like a container I told you already so now I will just do a control s okay and now what I will do I will go back to the dev config oh sorry I'll go back to the after Java let's just run this hoping for some new exception at least not the same exception okay okay now there is something new looks like entity manager Factory is initialized by jpa which is the cool thing and it is saying you need as default what do you mean by unit as default unit unit as default means guys lipica was just asking me these questions few minutes back so whenever we are creating a entity manager Factory it is getting created from a specific data source now this is the data source this is the database we want to connect this is the package that it has to scan uh this is the adapter or vendor adapter that we have given now we can have many entity manager Factory in our application which uses different different types of data source let's say I have two three different different database one is Oracle one is a MySQL one is something else then maybe I will have three data data source I'll be having three entity manager Factory bin right so right now you can see right now this entity manager that we are having here right here this is created out of what out of the entity manager Factory who is that entity manager Factory this guy right so you can see where we we are just having this one entity manager Factory for the persistence unit default now we are creating the entity manager Factory out of this now this is what we call a persistence unit I can set up the entity manager persistence unit here set persistence unit name and I can say I can say anything let's say MySQL uh something MySQL DB or something I'm just saying something okay so this is the unit information where I have all this stuff configured as per as per the MySQL stuff I have configured over here to connect to the MySQL TV so this is the this is what we call the persistence unit we have not specified anything that's why it is saying default just wanted to let you know if you're gonna come to here this persistence context has been created from the persistence unit what let me just it mentioned it here it has been created from the unit whatever the unit name you have given there just give it over here if you don't want to give also that's fine but just want to tell you this persistence context we are creating out of this persistence unit MySQL DB what this persistence the uh what is this persistence name persistence name we have just given a earlier's name to this set of configuration that we have done you can just make it anything okay that doesn't matter right if you don't even give anything this will be like you know um this will be like you know default or like you know default over here now if I run my application one more time you will see like this persistence unit will change to my SQL DB will get the same exception okay you can see and now we are using the persistence unit MySQL DB our entity manager Factory has been initialized but there is a problem why there is a problem because this persistence context annotation that we have used what it is saying try to understand now this is the question uh you you asked me now try to understand here what is the problem transaction required exception no entity manager with actual transaction available with the current trade cannot persist cannot have a cannot process the persist call it is saying in the Java uh line number 22 in the student dimple class click on that this line is causing me the problem why discussing with air transactional I have to use why why I have to use a transactional because guys whenever you are using this persistence context right so there is something which has been set up by default there is something called a persistence context type the default is transaction so that means if you are using persistence context if you are using entity manager by default is setting the transaction need to be activated if you are saving something okay so the transactions type is automatically set over there so I have to use it transactional annotation so whenever I'll do a persist the begin will happen from here the transaction begin will happen from here and the commit will happen after the method end right right sure okay so cool so now I have a transactional here right can I run this right now will it work foreign okay this is not spring boot right the transaction you are writing here and automatically the transaction manager is getting set up for you no it will not happen you have to you have to configure the transaction manager right so there is uh there is something called a transaction manager if I go over here called transaction manager come on do you have it yeah jakarta.transaction uh spring framework.transaction okay let's say let me go to here okay this is the implementation anyhow the transaction manager guys this will help you to set up a transaction but this is an interface what is the class name Ctrl T you have lot of transaction manager right now jdbc transaction manager hibernate transaction manager jpa transaction manager JTA transaction manager what I will be using I'm using jpa right for jpa I don't want to use hibernate or you know jdbc directly I don't want to use that so I will either use this JTA transaction manager or jpa transaction manager this is a uh like you can say transaction manager they have another class in Spring called platform transaction manager which is the interface now for this we have few implementation jpa transaction manager and JTA transaction manager so so uh so basically I have to do this let me go over there I have to create another bin called transaction manager okay a transaction manager okay this one ah transaction manager I can use or I can just use the other one called platform transaction manager if you go to the platform transaction manager it is extending to the transaction manager this is also interface right but it has few implementation jpa transaction manager JTA transaction manager which one I'll be using yes this is the interface why you are saying JP now tell me the reason uh for example if we use type JP we can uh like whenever we change to uh like a library I understand it however you don't want to go hibernate specific so you are choosing JP or transaction manager why you haven't told okay well let's go ahead and use jtr transaction manager so what if if I'll use jda transaction manager do you think there is any difference between these classes guys anyone do you guys feel there is any difference between the class what transaction you know transaction manager job is to give you the transaction Management Facility to your application you can use a transactional okay that's good now tell me when should you so here I will be I can create any I have two classes right I can create a jpa transaction manager jpa transaction manager jpa transaction manager is equal to new JPI transaction I don't give a damn what code they have written behind the scene I will just return this I will just return this class right cool it it works right and here I just need to tell my to the jpa transaction manager I have to sorry what is that jpa transaction manager I have to just set the entity manager Factory okay and my entity manager Factory is this guy right control C just give the bean name of your entity manager Factory that's it okay uh or or if you want to do this that also fine if you're directly calling it but if you say you have already created a bean right why you are calling that method directly okay you can just write entitymanager Factory over here it will be automatically autowered entitymanager Factory um unless the entity manager Factory and give this entity manager Factory over here you are already creating the entity manager Factory over here right so that means that bin is available and this will be automatically Auto wired over here whenever you are writing any thing in the parenthesis whenever this bin will be created this guy will be automatically where this dependency will be automatically injected when spring will call this method they will inject this dependency right so you donate to even write out aware if the parameter is there you are writing admin so when spring is creating the bin for this this object will be automatically injected and you are basically giving it over here and the same object from where spring will be injecting this because we already created the object for the entity manager Factory right here cool so my transaction manager is created but my question is that I asked to Vijay that why I am using jpa transaction manager I could have used JTA transaction manager also right remember that it's not like I'm blindly choosing jpa transaction manager I'll be telling you the difference whenever you are connecting to one database use jpa transaction manager whenever you are using multiple database right let's say you have different different databases go with the GTA transaction manager there are some differences also we'll talk about it so it's not like I'm blindly choosing one for now as it is the demonstration we're just using the jpa transaction manager later while we are going in depth we will be just you know doing other stuff right so remember that I'm creating my transaction manager bin over here okay this will help me to manage my transaction management transaction let me go to my app.java one more time run this this time my transaction is also there let's see if it is working okay I've set up my transaction manager okay still we are having the same problem you have to enable it in the exactly exactly exactly exactly so right now the transaction been I have created but I have to tell spring hey go ahead and use transaction manager so enable transaction management right control s control shift o okay finger crossed I think we're almost done Java application come on man okay uh no been be not required type exception uh student but actually it is found okay okay okay okay so it is saying we are directly calling the there is some proxy related exception I don't want to go in depth of it follow my proxy session the problem is I'm directly asking for the implementation of that okay let me just change it to student Dao let me just do it student Dao dot uh student Dao dot class okay now it is automatically going to give me the implementation object right because for the student Dao the implementation is student adao impul right so I'm directly asking for the implementation object previously the student pull object now asking directly the interface class it will basically give me the student impulse class only right let's see right now let's run this no been named called student Dow available okay uh okay okay okay okay okay okay that is let's say student now student uh no bin name available let me just go to the straightened out impul this is my student now impulse once again this is my student now impulse can I just come over here go to my application.java can I just say student.imple.class okay um student.imple the bin related issue okay one second can I get the student Dao bin like this run this is it so then we have the classes that's the name equal to bin name equal to we can give but yeah if we are not giving bin Name by default the student our impul name will be student s small right uh so student Dao okay okay let's just do one thing let me remove this okay we will figure out that later okay now we have our student our impulse right no problem we are not implementing that interface right uh the student interface so that's fine okay uh Ctrl C go to the after Java report back my code should end our impulse some basic spring we're messing up okay student our Sim student now impulse okay now my class name is what student dot impulse copy this go to my app.java give my bin name here student now impulse okay now we are just asking for the student now impulse class get me the student.imple object and I'll just say student now okay and I will just do student.imple dot sub student right now spring you are happy right we are not using any interface so we'll go we'll go into that why we got that error right we don't want to mess it off right now so on this there we go come on man reconcept now let's see whether it is actually set run this there we go okay now we have successfully injected a the record to the database right and we have used all sort of configuration we are asking our container the student our impulse getting the student object calling the saved method and this their method is doing the work for me right now okay understanding it what configuration that we have done but now now one more question that I have okay so far like you guys have at least some idea right what we have done guys yes no like you have uh config uh the methods we can write in app configuring uh Come Again Come Again DV config method you have created another class ah yeah yeah because I just want to maintain the standard you can have one class also but I want to keep my data if you want to even uh if you want to even let me just bring in this app config to the config folder if if you want to even you know create an another configuration file for data source another configuration file for entitymanager then also it's fine okay not a problem yeah yeah any other questions any other questions what do you have what you have in your mind right now so far yes I want to hear your feelings yes yes we instead of uh JTA something left yeah so basically uh we're gonna be having a complete session on that for for now you understand jpa transaction manager I have used because I have only one database okay I I am connecting to my mySQL database right that's it right but later we're gonna be using multiple data source multiple databases that time we'll be using JTA so if in your application if you have multiple databases then you go with a JTA transaction manager okay thumb rule but right we're gonna be having so many conversation don't get me wrong uh my statement wrong but yeah I'm gonna be proving why I'm saying this thing okay [Music] so basically that time the local container entity manager Factory basically did it will not do that it will not automatically do that only when you are following the spring Booth structure then only it will do that otherwise you might have heard about The annotation called add entity scan you have to use that annotation I'm going to be showing you that whenever you are creating the entity manager fact just remember one thing guys be very serious about this entity man NTD scanting the entities that we have in our application the the entities that we have over here with 8 entity that that thing will not be automatically considered as like you know like your application com the context scan although what do we say that the component scan will not happen with this right you need to make sure your application context is also holding the entity and for that when we are telling spring to create different things for us uh one second guys just just be on the line if you have an if you have office you can drop up but I'll just continue discussing for 10 10 20 10 15 minutes more just give me a minute guys yes everybody here so guys if you have hobbies you can drop up my sister is calling because he has to go to her office I have to drop off but yeah but I will take I'll talk for another 10 minutes guys because I don't want to leave you guys in the Midway I want to show you one more thing but very good question here see this entity thing you have to tell your entity manager where this entity things are there it will you have to do it how by writing this packages to scan you have it right uh packages to scan where we had that the dev config file right here if in springboard if you you also have to do that using a special annotation called add entity scan I'll show you how it is right we'll be going into that so okay so far I think you have little bit of things in your mind but yeah I will just show you two magic thing over here okay now the the yeah yeah [Music] main application yes yeah so do we do we need to do that object to create like student I will ask equal to news today yeah that object you have to create yeah you have to create that object because that that object you know is a object that you want to persist yeah so it's entity class okay that that City but it's but we are creating that student to save exactly exactly now here is I have few questions for you okay just let me have little bit of water um [Music] how are you first you tell me yeah [Music] no The Entity will not be components can uh basically um uh chaitanya you have to use a special you know your components called answer is principle because that local pin also is come from Supreme yeah but but but spring does not this uh entity will not be scanned using this component scan just remember that much okay it you need to use best packages to scan the way that the that I have told you otherwise there is one more way to do it I'm going to be showing you that right now here few things quickly let me just try to uh try to complete let's see if I can do it in 10 minutes uh but yeah if you have office you guys can drop off uh okay let me let me just show you a few things now guys tell me two problem we had so far not two problem which is not result the first thing the first question might be okay well as you are using persistence context why can't you use at Auto ad right first thing is that I want to use that automatic Vijay was asking me right I have to use that dot over then why can't I use it question number one you know that if I'm using autoware I'm going to be getting an exception it will not work right it will tell you right that the object will not be created blah blah blah unsatisfied dependency expressed through entity manager blah blah blah okay first question is that so I want to use entity manager like this uh Auto word I want to use then how can I do that okay and I fill for that for that I have to introduce a guy and that guy name is spring data jpa it's not only because I want to use Auto word here and I want to do some more cool thing I will tell you in in a minute and this is the most interesting part I'll go to the palm.xml right now I want to bring top those spring data JPM because you know it's special right now for us so spring data GPA go to that spring data jpa click on this 3.1.1 go to the spring data jpa copy this guy and paste it over here this is the spring feature of the GPA okay and we'll see how it is going to help us now we go to uh the after Java okay run this one more time nothing will work the same issue you're going to be having but I feel like you know you have to do one more thing you have to go to the dev config hey my recording is happening right okay so you have to go to the TV config and you have to use The annotation called it enable data what is that enable data jpa now enable JPL Repository I'll tell you what this guy will do this is a very special annotation that we have and this enable JPI repository will do magic for you right now I'll show you how okay now let's go to after Java okay I think we are ready okay no uh entity manager fact okay okay now you see now whenever we are enabling The Entity manager like you know enable data GPU over there spring is telling me something no bin named entity manager Factory is available so it is saying entity manager Factory bin is not available but we have already created The Entity manager Factory being over here right so now we have to do a special thing here uh just change the bean name to whatever it is saying okay because spring is looking for this thing behind the scenes doing certain stuff it is needing a entity manager Factory bin so my bean name is different if I don't I don't have any beam name for my entity manager Factory so by default what is my bean name anyone here what is my bin name in this bin and if I if I want to change my bin name to entity manager Factory then what should I do [Music] what is the second option you can change the name of the method exactly thank you worked right worked let's go over there we go second record now the autoware is working right now the auto air is working right right here it's working okay one last thing I cannot I cannot go without saying this thing I know my sister is gonna kill me today because I'll be late but yeah one last thing okay the cool feature of jpeg you want to see now see uh you are riding the save student right right here by yourself because you want to save a student what about if you want to if I'll tell you okay help me to write a method which will help me to delete a record then how will be doing it if you want to delete a record then how will be doing it if you want to find let's say I'll tell you okay help me to find all the record from the database then what you will do you'll come over here you'll write a method right public void yeah public list of student and you will be writing like what find all student and you will be returning the list of student you know what you'll be doing and again you cannot be using entitymanager dot uh dot dot dot find they will be giving you a specific student maybe create query you can use right what is that create query create query you have to give your query name from a student that is your entity name jpql query you have to add then you have to use you have to write student student.class it is going to give you a typed query assign statement to a new local variable typed query this is the query that you have created then you have to run this query by doing query dot get Resort list and this is gonna help you to return the list of student control 1 assign statement to a new local variable this is your recharge layers taken written this right cool now you can call this method this is gonna get you all the students that you have right can we test it quickly copy this app.java can we come over here uh I want to call this method you can just use this Tau import dot find all Ctrl s relaunch the application okay record inserted or we are not printing it Ctrl 1 assign statement to a new local variable find all student um let's say this this one I'll say all student and I can just iterate over it so I can say all student dot for each and I can just do student and I'm going to print all the students search this out and um student right now this is going to help me to print all the students that I have in the database so this is another okay what what is the issue what is the issue here students is out change oh the GRE is is 1.5 right now I can go to um okay I can go to um perm.xml and I have to get in inside the properties I have to add my compiler level I'll go to your uh let's say Java compiler maven Java 8 compiler and I'm gonna be copying the source copy this couple of things Ctrl C go over there and paste it over here changing it to let's say um 17 or what is that uh let's let's let me use 17 here or 19 here okay 19 Java 19 I want to use Ctrl s and now I will just update my Maven update project let it use Java 19 here Java 19 so I'll go over here to the app.java and I think it is gone now let me just try to launch this and this will help me to print all the students is it yes now all my students are getting printed here which is cool here I have written a utility method by myself isn't it find all by all student now like that if I tell you okay find me a specific student remove a student delete a student find a particular student by ID then what you have to do you have to keep on writing all these methods for me right you will be writing the method you will be writing the save method you will be writing the code you will be writing find all student method you will be writing the code how basically the student will be faced from the database that's what you're gonna be doing you as a developer will be doing that if you don't want to do if you don't want to do this okay then this is the cool thing I will just okay I will comment it out everything that I have now what I will do I'll go to a Dao class just create a down interface no need to write anything inside here remove everything okay no need to write anything now I will Implement something here implements uh implements or exchange sorry extends repository okay two things you have to do first The Entity name what is my entity name oriented and Ms student right student this is your entity name just Define your entity name inside the diamond bracket and what is the type of your primary key my primary key is ID and the type is integer copy that go to your um the Dow class that you have created okay and Define The Entity name and the type of the primary key now my entity name is student and the type of the primary key is integer now everything right lot of methods will be given by Spring only I'll tell you I I have I don't have any Dow input class right I have commented out the code I have a simple Dao class and now I have X I have given it a superpower by implementing this interface okay I just told you that when your interface student Dao extends to the JPI repository your interface will get some superpower what kind of superpower the superpower is you don't even have to write a single lineup code to do any crowd operation with the student entity right for the student entity if you want to save a student delete a student of data student merge a student remove a student for whatever you want to do this JPI repository will make sure to make those methods available for you now you don't have to write a student Dao implementation like you did in couple of minutes before you don't have to write this class so this kind of code is not required so for an example if you want to write a save method well you have to write this much of code and this is not required if you want to find all the students you have to write this much of course word this is not required why it is not required for an example right now you have a student Dow impulse because you want to save student daily student update student blah blah blah so you are writing the same method you are writing the final method and blah blah blah but what what about if you want to write a employee Dao impulse what about you have to write a book d a or impul what about if you want to write a Dr Dao impulse you have to do the exact same thing you have to Auto aware The Entity manager and then you have to save the employee you have to find all the employee for the doctor it is also same save the doctor find all the doctor for book it will be same save the book find all the books from the library it's like you have to you have to call the same purchased method for the student entity same purchased method for the doctor entity if you want to save a doctor object save same persist method to a lawyer entity if you want to save a lawyer same for the final student method as well if you want to find all the doctors if you want to do find all the employees if you want to find all the teachers then you have to do the exact similar kind of thing and that's what spring don't want you to do spring is saying that no need to write this impulse class right now because I will create the simple class for you and what you have to do you have to just extend to this jpa repository and you have to make sure you are telling me for What entity you are trying to generate the Dao impulse and I will basically create a input class for you on the go on the fly right and that superpower is coming from this interface called jpa repository and this is what we call a repository concept the repository the jpa repository is called a repository why because if you go to the jpa repository if you can go to the hierarchy you can see the jpa repository extends to the list crowd repository the list raw depository extends to the craw repository the crowd Repository stands to the repository so repository is a interface is the marker interface we're going to be learning about lot of different things about it but you understand the spring will create the implementation for all your repository classes right now your interface that you have written call student Dao is also a repository interface because it's extends to jpa repository and JP repository internally extends to the repository so spring will take care of writing the implementation class for your repository interface calls to den Dao right and inside that implementation class so spring will generate the implementation for this the inside that implementation what spring will do spring will just write the same methodologic delete methodologic update methodologic and blah blah blah methodologic so that you can do Cloud operation with your entity call student why spring is going to write that because if you want to see if you go into the jpa repository you can see you have lot of methods like flash save and flash a lot of things like you can see over here uh delete something in batch then you have a lot of methods like find all okay and if you go to the jpa repository is extends to some other interface let's say list crowd repository if you're going over here you can also find a lot of methods here called find all find all by ID right if you want to find all the you know objects which are available in your database through ID if you go to the crowd repository which is internally extended by the list Rod repository you're going to be finding some more crot feature like say saving an entity saving all the entity finding an entity by ID um if an entity exists you can find it by Exist by ID method you can find all find all method is there right find all by ID method is there so uh there is a count method there if you want to count how many records are present inside the database so there are a lot of methods present here inside all these interfaces that you're going to see like Cloud repository which is extended by listcraw depository list called repository is extended by jpa repository and JPI repository is extended by our interface so as our interface is extending to this all those methods are coming to this this interface for free of cost now you might say okay I will ask okay I can see this JPI repository Also let's consider this but this is an interface right it doesn't have anybody let's say flash method it doesn't doesn't have anybody let's say list crowd repository let's say crowd repository if you are going over here let's say you have a save method I understand that the same method they have written so you know I can use it to save an entity but where is the logic well the logic as I said you will not be writing the logic the spring will write the save Logic for you you can see the entity what is the entity Type S what is s s can be anything s can be anything any class student class doctor class employee class lower class book class pain class remote class mic class camera classes can be anything if you want to save a camera this s will be camera if you want to save a student this s will be student if you want to save a mobile phone this will be a mobile phone right so what I mean by that you just need to tell your Dao class that okay here I want to save a student or I want to delete a student you just need to write your entity name over here and accordingly spring will basically generate a implementation class for you called student.imple and will give you the logic for saving student deleting student removing student updating student and blah blah blah and that implementation class will only be generated by spring when your Dao is implementing the repository interface and as I have told you the jpa repository internet early implementing the repository interface and that implementation will be generated but it is not going to be that straightforward it will only be generated when you have that annotation called at enable repository that you have already put inside your DB config file right so when you have that annotation then only the repositories implementation will be generated by Spring and you can happily use it how let's see that I have created a interface extend it to JPI repository it is a repository interface and now what I in this repository interface is there inside which package this repository interface is there inside this package right uh com dot selenium Express whatever whatever okay double package copy this package name go to your DV config okay and wherever you have done enable repository just give the package name here base base packages and give the package name of your repository over here which is down now you're going to be seeing magic what spring uh the enable repository is going to do now go to the app.java okay now we don't have any student Dao impul object right here now we can go for student Tau okay student Dao and what is my interface name student Dao what it is going to give a student DM yes JP repository right yes on top of that class can we write at the rate repository no no I have not did I write it no I told no this is jpa repository I'm con I'm not writing any depository annotation nothing I have I have written nothing here right so I'll go to the uh where I was I was inside the config file a DB config file right I have given the package name where I was I was inside the app.java okay so here I have got the student now now see you want to save you want to save the student now take the student out student Dow is what an interface do you have anything inside it do you have written any code inside it no you want to do a sales right go back and just do Dot and look at that how many methods you are getting isn't it cool you haven't written anything delete my ID get by ID you find all uh plus a lot of different things you can save you know I want to save something let's say save okay the same method who is written did I write no I haven't write anything I want to save put this object over here what you want to do you want to iterate right I have removed the student now impulse right you want to find all the student so uh use this student now object right now okay now just to find all okay find all will give you a list of students right now okay look at that isn't it cool now just run this Ctrl s control shift o to remove everything let's see if if we are getting a desired things done see now is uh inserted the record the record is inserted if you don't believe me just write another another thing let's say I'm inserting a recall as a Rakesh and let's say hyd right and this is Rakesh okay creating a new object called Ruckus saving it okay and then I'll save Rakesh yes I'm changing it to rakes let's just run this rackets will be inserted see all these things Ruckus is inserted and I'm also getting this data right practice is inserted let's just check see Ruckus is inserted so what is the cool thing over here the cool thing is we don't have to write the methods right now spring is automatically generating the repository for you why because we have written at enable repository okay now spring is generating the repository for you and we have just written a Dao class we have extends to uh the jpa repository and that's it we don't have any methods let's say I want to find a specific student okay go to the update Java okay write a method or you don't have to write any method just just use it you have the student Dao right what is student do the same interface that you have the same interface that you have you have no code inside it go back and just to dot find by ID okay what what id you want let's say a case record I want number six come over here put number six right and uh right here Ctrl 1 assign statement to a new local variable and you got the student right it is giving optional back that means uh whenever you are doing six maybe there is a possibility that the number six record is not present in the database so I can just do student dot is present you can first check with the if block if the student is present because it's giving you a optional it is doing the null check by itself and right here if it is present this out uh student dot get right there you go uh let's say out and just skip some star Marks here finding student with ID 6 star Mark here okay controllers run this there we go finding student with id6 is Ruckus this method who have given us did I write it no we have given us the spring data jpa the spring data GPA has created a implementation class for this guy there is no code written by the developer but it has created an implementation class you don't you don't believe me put a breakpoint here right line number 23 start debugging right click debuggage Java application let the breakpoint step stop over here in the line number 23 okay do a switch okay go to the next step uh next step step over your step over step over on this look at that what is this a jdk dynamic proxy so the implementation class has been given the student now is what is an interface the implementation has given by whom by Spring it has created a proxy object what do you mean by proxy so what do you feel what is happening behind this in anyone what do you think what is happening behind the scene right now student division interface right but we got a proxy object as implementation so what do you mean by that uh there is the class called as um yeah basic no let's not go into deep we have a implementation call student Dao but spring behind the scene has created class implemented your interface then given all the different method that you are writing by yourself right here right all these methods like find all students save student find student by ID delete student remove student all those methods spring only has written for you you just did a guesswork okay just tell me two things tell me the entity that you are using okay now you are in the student now what I have done I just tell spring two things okay this is my entity name this is the ID of my entity the type of my entity uh the ID type of my entity and that's it spring has created the comp giving you the complete crowd feature right over here you can just do here you can just go here and can do write anything over here and it will work like a chart right so that's the thing that's the thing so let me just close everything so that's that's um I just wanted to tell few more things but I think it will not be possible uh but yeah also I think I think you understand right all this thing is happened by default by Spring and uh two things you remember this entity scan that you are giving the end the repository classes that you have written here this uh the repository class that you have written here this will not be automatically scanned by the component scanning and the entity class that you have written here uh in the student.java this will also not be automatically scanned by the component scanning so the entity scan thing you have to put it over here to your session Factory and for your repository classes like student Dao because I want spring to generate the implementation class for this and give giving us the implementation for that you have to go to the um you know what uh where is that a DB config right here you have to put the thing right here okay so this thing will not happen automatically the repository class scanning in the entity class scanning will not happen automatically and springboot is doing all these things like a magic okay how much time I have I think she'll be ready right 9 16 already okay I yes we'll be discussing about all these things in the next class [Music] [Applause] [Music] so all these things guys you have done here all is getting done by Spring but I will ask can you flash forward I'll try my best okay I'll try my best so guys see all these things is right now done by Spring only for you you are not doing anything right spraying is right now control C control V springboard when you are using spring boot you are not doing anything all these things are done by Spring all these configurations are done by spring so in Spring boot what you are doing you are not you are not actually using all this dependency right you are using one simple dependency all this dependency all this dependency you are not using data JP and all this different what is the only dependency you are using anyone you are coming over here it is spring uh boot a spring abort starter data GPA and the starters data jpa is going to grab all this dependency that you have added the provider the jpa the hibernate um like the spring contacts and everything spring jdbc spring orm everything this guy will get you okay and that's it and in the version you don't have to maintain the version here right in the starter data jpa or you also need a MySQL connector because you are connecting to the mySQL database so in springboard these two dependencies will be enough and obviously right now the person will complain me so how to how do I fix this person stuff I have to add a parent dependency right in Spring boot yeah start a parent so I have to come over here I have to come here and I have to go for springboots data pattern to look for that and uh I think starter test starter parent this one go to 3.1.1 copy the sky come back paste it here okay and it's not a dependency remove the dependency and remove the dependency and the type and there you go you have the parent and that's it your code is ready because right now everything will happen automatically so right here uh you have you have done everything right so in the source main Java right now all these things they can safely delete in the Dow class they can just delete this Dao impulse class not needed you can go and delete this configuration package not needed right and you need the entity and in the application.java here you have to just do a little bit of tweak what what tweaks you have to do container you don't have to create who will how it will create a container in Spring boot anyone guys springapplication dot run cool that's it you are done with the application spring application dot run in the run method two things you'll be providing what is that the first thing the the configuration the stator class name app.java this is going to be my starter class and the arcs this is going to help me to provide the container right so control one assign statement to new local variable this is your container right that's it you are done and the starter class to this starter class I'll tell okay I will not configure the data source okay I will not configure The Entity manager Factory in anything so you automatically enable the configuration for me right component scanning only you only do it everything you will you will only do so right now I will just use this spring application annotation it will automatically activate the you know component scanning it will also set up the data source and the entitymanager factory transaction manager everything by itself because it has the enable auto configuration right so that's it Ctrl shift over to remove any Imports that you have and I think you are done this is my springboard application right Spring application.run right click run is spring button okay oh the data source information we don't have over here so add a source folder new source folder and Source main resources okay and here just create a application dot properties file application Dot application Dot properties okay and here just Define your data source information data source data source uh what is that username right username and then password and the URL yes and the URL okay data source dot URL where is that let me just write URL here okay so the URL will be DB config the URL will be this copy this Ctrl C go to your application properties paste it username is going to be root password is going to be of the last right control s and that's it let's go to my um springboot project go to app.java run this yeah you have to give this hybrid or ddl let's not give it that's fine okay see it's working fine right everything is happening automatically the scanning the all the stuff is happening automatically you have just built the spring boot application by yourself now your job is go and activate the debug here to true and analyze all right guys so once you've activated the debug just check everything that you have configured there by yourself is automatically getting configured by springboard by the spring boot Auto configuration feature so if you're going to do a control F here and look for let's say data source you're going to be finding there is a data source order configuration see this data source Auto configuration this is the auto configuration class which is which is basically configuring the data source for you if you're going to do a control shift T and you want to go into this class you can see this is the automatic data source configuration which is basically helping you to set up some data source over here right I don't want to go into the code of that that is happening automatically by Spring spring has written the data source code for that and you don't have to configure the data source by yourself and for an example if you want to do Ctrl F and look for uh what else we have configured on our spring application I think the entity manager Factory if you want to go up you can find you can say initial realize the jpa entity manager Factory and the persistence unit is default that means spring is by default creating a entity manager Factory for you it is configuring it for you that entitymanager factory being that we have created right to a control F and search here inside the log you can see there is some Auto configuration called jpa base configuration and here you can see entitymanager Factory meshed and they are creating you the same Factory you can see local container entity manager Factory Bean this guy all right it's automatically getting created for you and uh you don't have to create a local container entity manager Factory bin and the condition you can see there is a conditional and missing bin that means if the developer is not defining a entity manager Factory by by himself then spring is going to create this bin for you automatically local container entity manager Factory bin like that what else we have configured do a control F with have configured transaction right let's say transaction manager see the transaction manager configuration is also matching you can see conditional and missing bin if the transaction manager is not defined um this bin is not defined then the transaction manager is automatically getting configured by this jpa based configuration class so you can go to you can look for the transaction stuff I can see uh See this is JTA Auto configuration so it is activating the JTA the JTA transaction manager is setting off we have configured the jpa transaction manager where spring is automatically setting the JTA transaction manager for us right if you're gonna keep on doing next next next you're going to be seeing like here uh transaction management data configuration they are activating the platform transaction manager and all these things say enable transaction management configuration is also getting triggered and that at enable transaction management that we have given right now spring boot is automatically enabling the enabling the transaction management for you lot of things it is happening behind the scene like the transaction manager uh entitymanager Factory uh the auto configuration for the data so all these things are getting triggered by springboot so basically if you want to test this you you already know the springbot application is already com is already containing the enable auto configuration so this guy is doing the magic but let's say if you are going to exclude the let's say the data source or the configuration then your application will not start data source order configuration.class if I'm going to exclude this order configuration then things will not happen so if I'm gonna resume this you'll see that your application is not not it's not not starting because now the student now being is not available because it is not able to connect uh to the database because the data source is not not available so uh we have turned off the data source order configuration now I can go to your springboot project you can and same way you can create a configuration file here in your springboot project create a config package and create the data source connection then your application will work if I will control Z this okay then my application will start normally if I'll run this it will start as usual it will work okay but here if I'll come here what is this um entity manager it is setting off the JTA platform manager JTA platform implementation for my transaction management entity manager Factory it is uh setting up by default right do a control F do find look which class is setting of The Entity manager for you uh local container hibernate jpa Auto configuration you can see there is a class here which is setting of The Local container entity manager Factory bin okay look for the class which is doing the auto configuration you can copy this and it can just go here and can do a exclude then you'll see that your application is not starting because this utter configuration you are turning off then it is expected that you will be creating the config file and you will be writing the uh you know configuration for this local container entity manager Factory Bean you need to write the bin for that manually so you can exclude this you can create a configuration file and keep checking on the same thing that we have already discussed right so just wanted to let you know that the auto configuration here is helping you uh so that you don't have anything inside the springboard project see you have only a simple Dao class and this Dao is getting scanned by your application um uh the springboot application stuff is doing taking care of everything and also you have a simple entity class you have nothing else you have no configuration file here but still all the configurations are automatically done for you right but as I said here you can see the entity is automatically getting scanned and the Dow is automatically getting scanned right but I told this couple of things will not be done by the component scanning if you know in the app.java the spring application has a component scan right it has a component scan enable component scanning where is that Auto configuration springboot configuration component scan is there but this component scan will not be scanning your entity and the Dow classes remember that here it is scanning because we are following the springboard project see the root project it is starting with what data jpa intro then we are having data jpa intro.dow data JP enter dot entity but let's say I am keeping my entity class in a different package for an example here I will create a package called let's just create a package here simple package uh two package I'll create let's say this is my NTT package okay and I will create another package called uh Dao right that time the automatic scanning will not happen if you want to keep your a student inside the entity package let's just do a okay I'm sure that your student data will not be inserted to the database because this entity will not be scanned if I'll resume this see we are having a problem entity manager Factory could not found uh and see we are having some issues here it is saying no Bean called entity manager Factory is available oh this is happening because in your app.app.java we have excluded the entitymanager fact let me remove this let me launch it and you will see like the the thing will not happen see no no manage type called student this this student entity is not available because right now spring is not able to scan your entity right this question only you are asking right so now this entity cannot be scanned so you have to explicitly go to amp.java class and then you have to use a annotation called entity scan uh and then you have to Define your package here for an example my package name is whatever Define it here this scanning The Entity scanning will not be happen by this component scan that you have just remember that this component scan will not detect The Entity annotation now if you want to resume I think it will work now if you are resuming yeah your student is basically inserted to the database right it's working fine so this this is one thing that you have to remember same like that if you're gonna go to the Dow package and if you're going to move this dial to this dial now as I said this Dao is having um you know is extending the jpa repository right this JPI repository is also going to be scanned by springboot right how it is scanning we're going to be talking about that in our future lessons but I think if I'll go to the same application and if I'm going to be launching this I think I'm going to be having problem see it is having problem no such being definition called student Dow is available because I have moved it to a different project which is not you know which is not matching with the spring boot project that I have the app.java is where the app.java is inside the data JPI intro package so either I have to keep my Dow inside this or I have to keep my Dow inside any any package which is getting started with this this name space like you see this dial package it is inside the same name space where my app.java is right so now the Dao will not be automatically scanned then you have to again go to the app.java and you have to write it enable report jpa repository and you have to specify here Tau and then only your you know the the proxy will be created for your task okay so now I have only couple of dependency here one is my MySQL private dependency and one a is my spring put starter data JPI dependency and this is the magic dependency that we have this dependency is going to pull in everything for you and that's why it is a starter data GPA dependency it is one of the starter project of springboard so springboot don't want you to go and look for all the dependency by yourself like we did it will pull in everything by itself for an example if you want to go inside this dependency you can see it has everything see it has spring boot starter aop whatever spring boot feels like it is needed it has spring boot starter jdbc we have seen like you know we are using jdbc also in our project you remember I have pulled in Spring orm and inside spring orm we had spring jdbc and inside spring jdbc we had driver manager data source and that's why this boot status jdbc project will bring in all the dependency which is related to the spring jdbc stuff and also it is pulling in the Hikari connection pooling see it is by default giving you a connection pool feature you don't have to you know create a normal driver manager data source kind of a thing spring boot will even create you a better connection pool and also if you're gonna come back here you will be finding your data jpa dependencies here you have spring aspect dependency you have hibernate core dependency right all this dependency has been pulled in by Spring boot starter data jpa project so this dependency from our project is it it is the One Stop solution for the developer where you're going to be getting all the dependency that you will be needing that's why if you'll be going to the map and dependency section see all these different dependency that you have which is needed to build a data JP application in spring so that's why uh you know the spring data jpa not only bought you the data jpa dependency for your you know repository creation Auto automatic repository creation it also brings you the hibernate so spring boot internally uses hibernate for uh for the jpa provider and also it is just the jdbc and also it Springs in the aop and the different stuff right one last thing guys whenever you are practicing if you are using the normal application normal spring application whenever you are creating the log The Entity manager Factory if you want to see the properties uh for an example uh this hibernate adapter right you have it over here create the hibernate adapter separately like this Ctrl 1 assign statement to a new local variable like this and just give it give this guy to here and if you want to set the you know if you want to show the SQL property you can just set it to here hibernate jpa adapter dot show SQL and you can make it true whenever you are debugging you can do this in the normal spring application if you want to show your SQL thing right this is my normal application right now the springboard application so I can just go ahead and run this it will print the SQL how it is going to like you know you can see the insert queries are getting printed okay and the same thing in Spring boot also you can do in the spring boot in the application.pripties file you know what you have to do you have to just activate the show SQL so whenever you are debugging uh just activate this couple of property just to make sure like now the things are working fine or not so go to the springboard project run it as a springboard application right click spring application and you know it will just help you to print and log the different things the insert queries are here the select queries and the insert queries are here okay that's it guys I have to go tomorrow or tomorrow we don't have any sessions Tuesday uh sorry Wednesday we'll be meeting again that's it guys for today thank you very much and I'm sorry I'll not be able to take any questions right now okay thank you I hope you understand that I'll see you guys thank you thank you guys for staying with me for around couple of hours I think we have done few things today okay so I do think guys this session is okay like you I know this is a little bit of you know internal stuff but do you think um yeah I think it is good for understanding so you have it you practice it but not required not required to understand all this thing if you are a very beginner just ignore this otherwise just continue from the next session we'll be talking about a lot of different things okay um [Music] sorry it's already they are sheetal uh on the springboard three uh the new repository ask in the group and now these people will give you the recording uh link okay thank you thank you guys uh just ping me your questions on the on the chat please you know I have to go thank you guys have a good day and yeah see you guys on Wednesday bye okay guys for the final time all the new recordings are going to this course okay so all the 2023 the springboard three recordings are going to this course and you can come to here to the last section and the spring data jpa starts from here so today's recording will be added to this playlist only do you remember that I will not repeat it so 2023 recordings are coming to here you can see this is only having around 60 hours of content but this will keep on growing so all the new people you know like for for your recording the 2023 people your your course is going to be this but if you have access to the micro Services course let's say then you can learn the micro Services course these are all the 2022 batch course right so you can you can go here and can learn about like different design patterns and all the stops in microservices but uh your sessions will be different on microservices and your sessions will go to maybe this discourse or maybe a other new course and another thing is let's say you know you want to learn the data jpa the springboard and all the other stuff you don't want to uh you know wait for the live sessions then go to this course and look look for the videos that you want to find for spring springboard spring rest hibernate if you're planning to learn hibernate then learn it from here learn learn all these things like you know it is going to be a step-by-step process right and for boot you go to the last page sorry you go to the last page here to the page number five and you start learning boot from here all right springboot starts from here if you are talking about the 2022 batch but uh if you are not planning to follow the recording you are attending the live just follow the boot from here because we have started boot already from here right I hope this is making sense yeah thank you guys [Music] [Applause] [Music]
Info
Channel: Selenium Express
Views: 17,329
Rating: undefined out of 5
Keywords: spring data jpa, data jpa, jpa, spring boot, spring data jpa tutorial, spring boot jpa tutorial for beginners, spring boot jpa repository tutorial, spring data jpa with spring boot, spring data jpa with mysql, spring data jpa vs hibernate, spring boot hibernate, jparepository, spring boot jpa, selenium express, spring data jpa mapping, difference between hibernate and spring data jpa, spring data jpa specification, spring data jpa repository, How Spring Data JPA works internally
Id: y7MZTqzyg5Q
Channel Id: undefined
Length: 130min 34sec (7834 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.