What is JPA? | JPA Implimentation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back aliens my name is Devin ready and in this video we'll talk about what is JPA and how to wave them in JP now before going for JP there are some prerequisites so soap understand this concept you need to understand what you need to know what is Java you need to know what is how to work with databases and how do we connect this to with your Bob JDBC because if you don't know how to work with any PC then this video will not make any sense to you and having a knowledge of hibernate will make will be added advantage but that's fine even if you can even even don't know open it that's fine we can still try to understand what is happening here so Java database entity bc3 requisites right and now we can jump into the concept so before implementing it we have to understand the theory of JP right so what JP a is so JP a stands for Java persistence API so what exactly this means so let's get back to the world of IT so when you say you are walking in this IQ boil or technological world we are walking with informations right we are walking with data so whatever we use it's all about data right if using Facebook if you are using whatsapp you're producing at the end of as application as well it's all about data right so your company is spending so much of money why because they want to work with data so if you want to work with data you use office right in self as we process data now those data are by default temporary data because your application is working with some data the moment you close the application you will lose all the data what you want is even if you're a patient side shut down you still want to persist that data somewhere where do we post our data so normally we can persist data in a file or we can pass this data in a database which is your DBMS or odd appearance you can say maybe MySQL Oracle toast yourself there's so many are given is available yeah so question arise how do we save the data so we have one way you have a java application you have a database you can you Teddy BC but again that's what so many issues which ad BC you know you have to fetch the data or so normally in Java we work with objects and then in the database well you have those and columns how can you save the object in data in the rows in column format and that's why we have this concept of ORM so what Oh our NS or basically means it stands for object relational mapping where you have a class so you have a class structure so then that class will be having some variables and then you have a table now if you can compare your class name will be a table name and your class properties will be your table columns so we can do that mapping there okay so we have to use Oh Adam now Oh Adam is a concept which is applicable for different technology it is there they fit dotnet we have for Java so that different languages are technologies with we can use other but the problem is to implement ORM in Java we need some tools right so when you say tools we have hibernate is one of the tools we have hi Pat is one of the tool we have top things so there are so many tools I've enabled so you can use any of this tool then you can make it work so using hibernate using iPad case you can build Oh RM application now the advantage would be if you're using hibernate or any oil and tool let's say you have an object yeah so let's say that also you have an alien object inside alien object you have alien ID in your name and in a technology for maybe you can also talk about the salary of alien and which laptop alien use or which technologies working for give it and discuss about this but then let's say you have alien object and you want to store that alien object editor base so in JBC what you do is you fetch one one badge when you store it that's not good that's a little bit of a right so what Oh Allen says is take the object and save it into DW directly that will be awesome so to implement why don't we have different tools so every company they want to use over and now the problem is we have different tools right so let's say today we are using hibernate and and their application is built using hibernate now let's say after 2 years you thought ok I buy this has some extra features and we want to use those features so you could switch your application from hibernate - I bet is is it that easy maybe your application is built on top link and you want to move it to hibernate is it that easy to switch between different bottom tool you can switch if they are India if they are using the same type of structure of same specification unfortunately all these tools they were having their own way of working they have endeavored independent right and that's where this Sun Microsystems they realize you know wait to have some common standards and that's where they introduce JPA which is Java was API it is basically a specification so all these tools maybe hibernate I better stop and they will all be in they all will be implementing JPA standards or JPA specification so that if you want to switch from I bet is to hibernate it will be easier because you're just changing the tool the implementation but this specification is still same yeah so that's the advantage of using JPA in your project but yes if you are building your application purely on hibernate you will face the issue of shifting but if you build the application based on JPA it will be easier for you to switch between hibernate - i betting's but hold out when we have JPA then why wait in hibernate now as I mentioned JPA is just a specification so if you want to work with JPA you do need hibernate or Arbutus or any of this for the implementation so if you are building a hibernate project then you are walking with hibernate if you are building a JPA project then either you are using hibernate for the implementation all eye patches you cannot simply make a JP application without any of this tool yeah so happen it is important even if you're using GPA the importance of JP is if you want to switch from one technology other to other tools you can do it very easily in fact in future if you are working on spring booth applications and then if you want to work with database we normally go for spring booth JP so this will be a first step towards JP in this video so let's try to implement whatever we have talked about now what am I do is I will get a new project so you can see my sprinter and at week a new project you and I will say this is a mammal project ok with maven it's not here so let me say mavin I want to get a maven project and this maven project I will mention I want to create a QuickStart project because I'm not building a web application here will sir next ok so it does take some time to done it from the Internet yeah so it is asking for the group ID I will say nobody is combed or telescope and the project name I will mention is JP a demo I can you can have any project name doesn't matter click on finish and you can see record our project here now in this project what I want to want what I want to achieve so it's very simple I just want main methyl yeah so I went to class which you have main method so that I can do on the application so even before connecting with anything I just want to verify if this phone works on this Eclipse works that's it that should be a first step something certain has a job application and it worked you can see we got hello world but then I want to implement a JPA type of thing now of course to save data somewhere because JPA will be helpful for Sarah Gadon right so we need a database and fortunately I do have mask you'll hear installed so you can see in my machine I'm using mask you can use any DBMS you you want that's completely fine you can use poast piece you can use Oracle there will be some modification we can handle that so I am using masks you are specifically I'm using masks will workbench to handle my mask you again you can work on command as well if you want it but on break I'm a good big fan of GUI so I have a workbench here and the return is name which I paid it is Naveen entire database I have a table as alien so have already done this before hand so that you know I will not waste much of time in the video so in this alien table I have those five records you can see we got one I mean Java to watch other big data three debit dotnet and fourth Anna PL MN not ml and then turn off Michael so we have this five entries there now I want to fetch this data using application or maybe I want to store some more data so what I will do is I will go back to my Eclipse and I want to create so first of all to fetch that data since we are using o RM tool you need to also have a class which will represent the table so let me create a class for you here so I'm going through here I will say new class and this class will be alien and the second finish so we've got our class and then in this class we just need to mention those three properties so if you compare we have a ID we have a name and we have Ted let's do that so I will say private int a ID then private watch string a name a name and then private string tag so I hope we only have three variables and that's why we have three variables we have a ID a name and tag so your columns will your variables in the Java class and then these three variables need getters and setters or the right-click here I was a source generate getter setters and okay so we got our class and record our gate as it is we also need our two string method so I will say so's generate two string so that if you want to print an object you can print the values of waiting show up into the high school okay so we got our class here that's that's cool right now what I want to do is I want to get that object of a lien or maybe I will try to fetch data from the database so what I want to achieve here is we already have those five requested right I want to fetch the data from database is it possible let's try so what I will do is I will say alien a equals u so when I fetch I want to specify some ID right so I will say hey i want the alien with name of it ID which is three yeah so when I say three it should find David and dot that but will it work so I would say hey JP a please gave me a nyan object with ID for now or no not for three now will this work of course not right B Allah we are working on java technology this is not Java this is normal English right how can they implement that here so to work with JP a we have to do some settings right JP a is not a Superman right we need to want me to make it understand what you're talking about so how many use JP here so there further care to do some settings the first thing is JP a has this special method for us find now in this point you simply have to mention for and then you have to also mention the class name so alien got class and then you have to mention four so find is a method inside JP a you have to pass two things the first one is the class name the type of data you're you're fetching and the the ID which is four but can you simply call a method without a class name of course not right whatever we have learnt job in Java till now as if you have a method if you want to call it from a some other class you need to create object of it all if this particular you can simply use the last name how do we access fine here now to use this find we have a special class or the interface which is called as entity manager now basically all the classes or all the classes which represents our database table in JPA is called as entities now in fact if you have worked on databases we have e-r diagrams right so all the tables are also represented by entities so alien class is an entity for us right so to manage those entities we have any D manager because in your application you might not have one table or one class you might have ten classes so ten entities so we need a manager to manage all those entities so yes we got one entry manager but question arise how do we instantiate it unfortunately this ID sign that's an interface we cannot even say nunu into the manager that's the first problem the second problem is if I say control space is not working you can see I'm not getting the the the package now the package is not there is because in the maven dependencies I have not mentioned that I want to work with JP or hibernate so as I mentioned JP a is only a specification right you need to implement that with the help of hibernate all I but is our top link whichever you like but I when it is will the top contained in this list so I want to implement JP they have a hibernate so we have to make sure that we do have hibernate dependencies in this project and how do we do that so I will open Firefox browser and where's that it's getting open and in this Firefox I will download the dependencies for hibernate now we don't have to actually a download one by one jar file thanks to maybe it gives you a concept of maybe repository where you can actually search for hibernate so when you search for hibernate it will give you a hybrid depository in fact you can just go to the website of rebel depository and here you can search for hibernate even that will give you the same same list it which is not give you the same register let's search for higher on it again it is giving a different thing so you see the first thing which is called hibernate or in functionality okay if you have seen my hibernating dash series we have already done these steps yeah so if you know hibernate this is very easy for you you can skip this way to skip this some steps and then go ahead but then if you have if you are new to oil and technologies just watch the entire video so that you to make sense so you'll just let pick up the latest one here we go let's pick up the five point two point eight and I will say copy in fact let's say copy let's go back to our file and paste so we got one dependency for hibernate and the moment I do that can you see that in rem in dependencies it will download all the different dependencies for you now it's also depend upon your internet speed how fast it should be mine is very slow today is taking some time now by that by the time it downloads it's the hybrid dependency we want one more now see if you work with MySQL you need a mass per character right before working with Oracle you need other connectors different DBMS have different connectors to work with and for this one we need to use MySQL connected as well so every time it gets downloaded I will search for massive ohonta the first one and I will download which version I always prefer this table one this looks stable there is no problem with this let's copy this one and go back to your paste and you can see we got high by dependencies and in some time we'll also get my SQL one because I just saved it it does take some time to get down with it and you can see we got Moscow connector as well now once you got these two two things we can simply say control space now and you can see we got regard the imports and I first Center we got editor manager belongs to persistence package yay so that's the power of of these dependencies okay now how do we care of it I went to identity manager now unfortunately we have to write one more step to implement that to create onset of entity manager way to create object of editing manager Factory so this follows a factory design patterns you know all the all the latest frameworks they all follow design patterns it's all about design finance so we need to use factories and pattern here you don't have to implement it okay - it or JP has done it for you if you want to create object of entity manager you need any divisor Factory so let's say e-m-f any object will do but in question eyes how can it rate on the top entity manager factory good point okay so what we will do is these are there's a method called as there's a Pascal as process turns persistence dot is it a plows or interface is it it's a class so persistence dot the method name is it's what get entity manager factory no on this one is it created in a factory yeah so the method name is create entity manager factory and yeah something here again what is that something will I will I will tell you that in some time but you have to pass something here now once you've got object of entity manager factory you can create you can use that object and in matter of fact when you can say create a new manager okay so you just should repeat what we have done we have painted two objects I did the manager entity module factory and now using e/m which is the entity manager factory you can say ii m dot find and you will get an object so what do you think done this will this work of course it should not work because something is missing now know where we have specified which DB miss you want to work with I mean we have specified it in a pom file which is this this prom file here but then how do your project knows that the app knows which device you want to work with or which database you want to work with all what you're using is possible we have to specify those properties somewhere right but then now normally when you work on different frameworks you know when you work with hibernate JP a JP a with hibernate or spring framework or story work we do specify the contribution file or the property files you can say so here as well we have to specify some properties where now we can specify those properties inside SRC so inside SR 72 special folder inside Java is it inside Java let's verify no inside mean I want a special food up on his sauces I will say fold up the folder name is resources / and inside that the folder name should be meta I and this is the exact path you have to follow so it is resources / meta Hanif and inside this folder you need to create a XML file you can make it specify the properties now it's a new and this should be an XML file yeah so let's say XML because XML is a fancy thing right and like we have properties files XML files thanks to Spring Framework we also we have Java based configuration so I will say persistence dot XML and here we have to paste that we have to use that XML code now in XML you can specify your own tags I mean the framework will have their own tags so in JPA we have a tag on its persistence and in that persistence tag you can mention a positon unit and you can mention all the properties what I'm talking about this thing so if you specify properties or persistence and inside this procedure if you have a positive unit now why unit is because let's say if you have multiple settings you might want to work with different setting for different applications or different behavior at different time so you can specify a different position unit and you can select one at a time example this properties they have a unit name as PU you can create another one as PU 1 PU to appear you can create multiple positing unit and then here you have to mention these properties so we have our driver name we have a URL user name and password now once you have specified all these things let's go back to app now how do we link it how do we link this code with our XML and that's where this double quotes comes into picture this is where you will mention your position unit name be you can be anything it can be ABC it can be my JP APU it can be my P you it can be P doesn't matter right but you should have you should have a same name and fault aside now this work everything looks cool right let's verify so what I'm going to do is I would right click here I will say one ass job application you can see we got eddo now what ll be got is it says are they going to locate per sister which is Ariel it's there the class is available here why it's not able to find it that's because when you work with JP JP a bicycling works with entities what is entity so we have a class and they have a table so something in between we have entity how do we convert this glass into entity or how do we mention that this class is not about closet identity so for that we need to use a special a negation here which is called as a DTD this this is what will make this class as entity and then we have to use at ID which will be your primary key because every table needs a primary key so two things the first thing the first thing is the entity and second one is at ID two links that's it now this world oh no that's verify I will go back to my app and I will say Ron and Oh something has happened and we I don't know with it is it successful the way you can verify this by printing the object right so if you don't this if you print it the object you will give you the output right and you got it can see that we got our alien object that we got a and for IDs for Anna and ml you can change the value film for inside you can also view value from the user I will say I would say - and - I don't know who is that let's run this and - is that simple yeah so this is how you can save you can fetch the value how can you save the value because I want to one also want to see how did we save it so what I will do is I will go back here I will say alien 3 equal to new alien and then I was visible some values I will say a dot set a ID as it says 6 maybe I will go forward 9 I would say a dot set a mean as maybe Maria and I will say a dot set 10 ok which take is emitting now I would say tech is hardware yeah and then once I got this details how do I save it now to save this it's very simple you can simply say e F now to fetch using find to say the metal name is persist you're persisting it and you can simply pass a and this should work let's verify let's run this code and you can see it is printing here but then is it affecting database let's go back to database via Moscow and ok so there were some issues I just I was just solving the issues that's when I insert this inserted in your policy on ignore that row and I would say it run this code let's verify is it Maria then no we don't have money other that's weird now the thing is many way you want to change data in a database we use something called as you know we have to as actions to manage acid acid properties so here we before saving we have to start the transaction and before and after saving we have to commit the transaction and the way you can do that is by saying 'i am which is the entity manager get transaction dot begin this is how it was start the transition for you and here we'll say e and dot get transaction and we'll say commit it's that simple yeah i know the video is bidding getting away then they down but then that's how it works so yeah so now will it work let's verify it should it should it should yeah so you can see we got the output but then let's verify in the database here we go we got nine Mariette Maria and we got Hardware simple yeah so now let's see what it thinks they have done we have syndicated an avid project we created this app class of the order ticket they already had that class here in this we have kiddies I didn't object and then took all this important one is entity manager and then it image effect way so using factor we can trade on it affinity manager which is you to specify the the persistent unit name which we defined in the XML file again this XML file should be there in the resources folder I don't worry I will push this project on the github so that it will access this project or you will find the negatives description a description area so that you know you can just pull it and you can use it for yourself make sure that you make those changes here the d URL the user name ID pass will be depend upon your machine is Italian you can use identity @id and then we have this property we have this hibernate dependencies master dependency now there's one thing how do we know okay let's see if you want to understand everything about JP because this is only this is only one video right so what you can do is I do have a place on hibernate so if you want to understand each and every step why I mean what are the other possibilities we have identities can we change the column name can change the or what if you don't have a table can you create a table by yourself you understand that those terms when you start when you start working 500 series so after watching this video if you enjoyed it different like button but then also watch how it entire series and then you will get the idea about all the power of overlap tool the main idea behind this video is if you want to understand JP a this is how you implement but then we can also do with with hibernate right why you have to use JP here it's because in future if you think okay let's change I don't want to work with maybe let's say if you don't create a JP a project or if you create a five minute project from start the problem is in hibernate I have to think what you do is hibernate code example this two objects will be based on hibernate framework or hibernate packages the thing is in future if you want to change it you need to change the so let's say from hibernate if you want to move to eye biters or maybe top link you have to change the source for as well but if you look at this project of project which we have built here even if you are changing from hybrid hibernate to I am it is you just have to change one thing and that is your dependency that's it everything remains same that's the beauty okay maybe some other changes as well but then that's so it it does it require on the minimum changes when you want to move from different LM tool so that's the beauty about JP I hope you enjoyed it don't forget like button and do subscribe for further videos
Info
Channel: Telusko
Views: 241,155
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, jpa, hibernate, eclipse
Id: otinfgwkMbY
Channel Id: undefined
Length: 26min 20sec (1580 seconds)
Published: Thu Nov 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.