Hibernate Tutorial | Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens monuments of is ready and in this course we are going to talk about hibernate now when we learn a new technology there are four things important the first one why do we eat the technology second one what it is the third one how to implement it and fourth one the advanced concepts and in this course we are going to talk about everything so initially we'll start with P requisites that will move towards why do we need it how to implement that and then we'll talk about some advanced concepts as well so just to give you a brief idea about hibernate hibernate is one of the best ORM framework available for Java now when you say o R and what it is it stands for object relational mapping now if you think about this software world or the ID void of course we have software's on the other hand we have data the main thing is data right and to access that data maybe when I say access we want to fetch data we want to save data we want to modify data and to do that we write applications and that application can be written using any language like C sharp Java Python your choice now if you work with this languages and if you want to connect them with dill pop database that's why we need connectors right example if we talk about Java and let's a master database you need a connector and that can be implemented with the help of JDBC so you as a programmer you have to write the application like let's say if you are using Java you have to think in object terms I'd everything is object in fact in Python eyes when you do that so if we write objects with right classes and then on the other hand in databases we have tables right I'm talking about the relational databases so you have tables and in tables you have those and columns now to connect that we write SQL queries right so from the object world or with the help of application you write SQL queries and that gets fired to work the database what if you don't have to write SQL queries and that's where this ORM frameworks are quite famous so based on your classes based on your how many objects you create it will define your tables and data and that's fascinating right and how do we do that that will be saying in this code so I hope you are excited make sure that you hit the like button and subscribe the channel so that you will get what updates on this so you should be knowing two things before starting with hibernate first obviously Java right so if you don't know Java you you'll find it very difficult to understand what I'm talking about those objects stuff those oops concepts so cordial concepts is very important for hibernate because we will be dealing with poles which is object we'll be dealing with in Hastings we'll beginning with a for loop and all those stuffs so your cordial concept should be should be good next thing you need is the SQL constant because see what we are doing in hypothesis we are walking with data right so we are spreading the data from the application and they are storing that data into database so so we have the application here and then we'll be having our database view so of course you should be knowing this part as well as the database part I mean not just basic I mean let's say if you know about select queries you know about table column that's not enough you should also know what is joining table means how do we create Samara key foreign key how to normalize our table and all those stuff so you should be good with SQL plus it should be good with your constant because C and also media applications will be working with some classes and one class educating object in the other class the B we need to pass the object from one class or the class we need to create the constructor we have to work with interfaces right so on the Java side you should be good with Java on the database I should be good with SQL how your database all those things work now if you have good luck this thing we need one more thing which is the connectivity between these two you should know how JDBC works so this are some liquids JDBC yeah so this is some liquid pre requisite for hibernate so if you know the things you are going to go for the for the tutorials but in case if you don't know this stuff that's very easy so in the description section you can see some links one link is for Co Java so again there are there are normally even very less videos for Co Java it is only 181 videos and you will learn goes out from that you don't have to whatever thing for that yeah you can watch till interface right and for masks you'll also have some videos and buildings in the description you can watch those videos the other thing till you know how to create the American foreign key and then for JDBC have one you have to watch two videos I think I can you will find that in description area okay so these are the prerequisite for hibernate I'm never doing this you're good to go for hibernate but in case if you know this continue with the next video thank you so much and this video we are going to talk about hibernate now we talked about hibernate it's o RM tool which is used for persist in the data now again just to understand those terms let me give an example here now we are living in a world when you make different softwares right and we can build all these softwares with the help of a language right one of the language which me normally use to make applications is Java right and when you make a software that software will be working with data of course now that data can be a normal variable or it can be objects so when you say normal variable I'm talking about primitive values let's say ain't flow to string and string but in float a double all those values are you might be having your data in the object format and when I say object format let's say you have a student object maybe you have an employee object and every object will have some data example let's say I have a class here so I have a class here and the class name is let's say student and in this student class I have certain variables let's say I have a roll number then we'll be having a name and let's say we have marks so in this student class we have these three fields we have name we have null number name and marks now if I create object of student it will be having three bit it will be having three variables wide which is no name roll number and marks so that will be one object so if I create object of student now it will be having three values I want to make sure that those values are getting stored somewhere because let's say if you talk about your bank account so when you when you deposit money in your bank and that data is stored somewhere why that you have deposit ten thousand dollars let's say if one day your bank says hey I don't know we lost our database we lost our date we lost all your data I mean will you you will not be happy right so of course whenever you work with data you want to shoot it somewhere and that somewhere is your database now this database can be anything it can be masculine can be Oracle it can be SQL Server maybe pause grace maybe Adobe where we have from DBMS to work with right so we use database so that we can store data and that concept of storing data is called as persistence right because by default when you talk about variables those are transient which means we normally don't store data in the inside database directly so what we do is we use we create variables we create objects and those are temporary data if you want to store that data we need to use databases right any of this not anomaly to connect your java application with database we use a concept of j DB c which stands for java database connectivity right so we use JDBC to connect your java application with database and the moment you say you are walking with JDBC and we have a database so for sure we need to use something called as SQL language right and not everyone is comfortable with SQL in fact people who only learn Java they might not be comfortable with SQL and and the moment you say hey you want to work with database now you need to know SQL so we wanted a solution where you can store the data into database but without using SQLite I mean as a programmer I don't want to use a SQL query that's the first concern the second one is if I have an object which is let's say if I have an object which is obj and this obj object is off student which will have three values I mean roll number lecture roll number one the name let's say Naveen and let's say marks equal to 80 I want to store this mass into database right or this this data into database so to work with SQL I mean I hope your family of a JDBC what we normally do is we write an SQL query of course in JDBC graph two four seven steps and then we have to write SQL query we have to say insert into student than values and then we will we'll provide the values here right so again we have to write SQL query what if what if in Java if you have this object obj here if you can directly store the data inside database simply by saying hey hey a framework on any framework save my object that's it the moment you say save obj your work is done what if you have a student object and if you are saying save obj and that data is getting stored inside database your job is done right but how it's possible can be stored an object into database directly because normally when you say Jade database I'm actually talking about our DB M s time of databases which is relational databases where you have rows and columns how can you convert an object into table now for that you have to understand a concept of o R M which stands for object relational mapping okay so we will be having an object will be happy we will be having relational which is your tables and then you have to map it now how it is possible now if you talk about the class structure now this glass structure is same as table structure right example if I create a table structure here so let's say we have a table structure here and in this table what we have is so this this is your student table and and this will be having three three columns the first column is the roll number the second column is the name and the third column is the marks right so we have these three columns so this table this class so this class represents your table right and the object let's say we have the first object as obj1 so this obj one will be your first row let's say we have one more object which is obj - now let me just do that here we'll say obj - and it will have let's say - then we'll be having name as let's say RT and let's say marks is 90 here so that will be your second row so if you have five objects you have five rows so one class is one table so class is the table your objects is your rows so if you have five objects will be having five rows right that means we are trying to create a relationship between the object-oriented programming concepts and the relational database system and that is possible with the help of Oh RM which is object so that is your java concepts relational that is your database concepts and we have to map it okay I can it doesn't it's not that simple the way it looks on the paper but this is what o RM means so the problem is o RM is just a concept how can you implement ORM so to implement ORM we need to use some tools which is ORM tools and one of the tool of ORM is your hibernate again hibernate is not the only tool we have we have I Bettis then we have what know we have JPA which is these standards and then we can also use one of the tools from or from Oracle which is stoplight okay so we have I by tase we have toppling we have hibernate so this are your o are and tools right so using hibernate what you can do is you can store your objects into database directly with the help of saved method okay now question arises how do we use a safe method it's very simple to use safe method we need to create an object of session okay so we can say session object which is let's say s so session object is a so we can say s dot save okay but hold on how do we create session for that we need to create object of session factory because using session factory we can we can we can create sessions and in session factory you have to provide those details the configuration for your database example when you walk with JDBC you might remember that we use we use certain steps wide we have to provide the driver name we have to specify the URL right so you have to configure all those things with the help of session factory again there are multiple ways of configuration we can use XML way of configuration we can use Java configuration way right there are different ways we can do that so we can be have XML we have Java configuration right so we have to provide your driver name we have to provide your your database your database URL which is the connection string you have to provide username and password in the vacation file so that you can load that inside the session factory object and once you call certain factory object we can work with session and using session we can save the object not just saving we can also use we can also use get method to fetch the value to fetch the data right so we have saved we have get method so we can use all this concept to implement hibernate and Trust mate is one of the awesome tool available which is ORM and again om is not just you know a technology which is which which came accidentally it was well planned so if you want if you are making an application where you want to persist data this is something which you can actually use okay again in the next video we'll be talking about the practical implementation of this how exactly it works so I hope you have liked this session make sure you go to my channel or subscribe the channel and like this video if you liked it and that's it that's about hibernate basics and in this video we'll talk about hibernate implementation so for to implement this in fact in the last video we have seen the theory of it right so again to implement this we can use any IDE we can use eclipse we can use NetBeans we can use IntelliJ for this video I'm using Eclipse okay now when you create project in eclipse we have multiple ways right we can create a normal project or we can create a maven project now the advantage of using maven project is you know it will give you the project structure and since we are using hyper night here you have to use certain libraries for hibernate and to use those libraries we need to wait to download those libraries from the internet and then you have to add it right in sure of that if you can use maven it will do that it will do that task for you ok now let's see how to do it so again if you don't know about mavin you can watch my video on my channel so you can just go to that is the Learning Channel and search for haven't video and you can watch that if you're not familiar with maven so what I will do is I will click on new and I will select ways mavin so we can see we got maven project here and we'll send next now there are multiple ways you can do this you can select so you can go for internal and you can select a maven archetype QuickStart which is core job application if you are making a website you can select a web app here but let's say if you if you want all this thing to be configured automatically you can go for all catalogs so that you don't have to select you don't have to do any for anything for hibernate you will be getting will be getting everything for which is pre-built so I mean select hibernate here and you can see we are getting MVC spring hibernate project so in this you'll be getting spring hibernate pre-configured and since this is your first video for I button it will not do that will go for the internal and will select Quick Start say Quick Start that's your quick start and click on next now since we are going for Quick Start which means we are making a code java application so we'll say comm dr. disco and the artifact which is d which is your project name i will give as demo hype click click on finish now once you do that it will give you a project we can see we call a project here which is our haven't now what we are going to do is we will be using our mask ul and inside my mask you'll I have a database here which is neon so I can see I'm using that and then this database neon I don't have I just have one table I guess so we have a certain table here not not no other table here so what I'll do is from my application I'll be having okay let's go back to the application here and we already have AB dot Java which has a main function let's see if this this app is running or not so you can see we got hello world that means this project is working but what I want to do is I want to create a class and we'll say the class name as let's say alien because I promote Indians here we'll click on finish now in this alien class I want to have some variable so let's say I will create private int roll number or not roll number alien will not have a roll number idea will have in an ID then we say private int a name that's here that's the alien name and then as you can as you know that all this aliens have a different color if you watch all your movies or or bonnet movies you can see agent been different colors so I will say string color okay now once we caught these three variables we also need get a status for those things so we'll say right click and say source we have getters and setters and we'll click on this three click on OK you can see we got our getters and setters now this is your bean right so we got a alien being here or we can also call this as pajeon or people of emily with poachers right ok so once we go out to was alien let's get back to an app where you was creating the object of alien will say alien obj equal it or not alien which I will say alien okay let me just go with the name here let make it a lien of myself let's say Naveen or maybe I just come in pivot all right I don't give a space for not not promoting my thing so we'll say alien now once you got the object of alien here which is Paris Co I will add some values to it so I will say telesco and you must have seen the logo for alien right or telescope ten is for logo so we'll say set a ID we'll say once no one then telesco dot set name will say this is Naveen and we set that is co dot set color and our logo isn't green color right so say green okay now once we have said all this values now what I want is I want to persist this data when I say persist it means I want to store this data inside a database because this data is inside your Cola application not inside your database now how do we do that now if your family with Co Java tanks or if your family with JDBC what we normally do is we write JDBC steps those seven steps which you love import the package then all those stuff now once you have done with the import statement once you have done with the connection once you have done with the statement object and then then you can write the query which is insert into a table which is alien then you have to specify the values which is 1 0 1 Naveen and green so you have to divide those you have to isolate these values now as our job professionals you don't feel that those things very comfortable write writing SQL queries what if someone says hey what you can do is you can use our method which is save and you can simply pass the disco there and that's it your work is done once you do this your data which is the disco where you have 1 0 1 I mean and green will be going inside database ok you might be filling this - not true right but hold on we will do this and it will work so when you say save this data will be stored inside database but is it that simple or not exactly because see when you when you use save method we have to specify this method belongs to which class of interface right that means we have to create an object object of it first let's do that but hold on which interface or class this say belongs to so the same method belongs to interface for a session and this session interface belongs to hibernate framework so if I say control space you can see we are not getting any option because we don't have any library you can see I have Evan dependents easier I don't have any library here which which is of hibernate so we cannot do that right so first you have to add at this dependents we have to add dependency for hibernate and MySQL both say we'll open up a palm to the XML file and we'll move towards your palm to XML file here where you have your code we have to add two dependencies now I'm not Superman here to remember all the dependencies right so the best way is to go to maven repository the official website and search for the hibernate dependencies you have to go for hibernate ORM functionality and the version I'm working here is four point one point six because that is what I've tested so you can use that again we have different versions the latest one is five point X you can also use that it has some new features which if you want to explore but four point one will build do for us so we just have to paste it here that dependency and you can see we got all the dependencies here now since I am doing this project for multiple times so you can I was having all this libraries in my local repository if you're doing this for the first time it will take some time for you so don't be tensed if you are take it is taking long long time for you we also need a mask you'll connect all right so we'll say MySQL connector and we'll select five point one point three six or three five which is or not three five I will be using one point five point three eight does the maximum in use okay and let's copy this let's go back here and paste you can see we also got my ass connector here now once you got these dependencies that get back get back to our app and if I say control space now you can see we got an option of o-r-g dot hibernate so that's the package we have to use with a session object as session equal to the problem is if I click on session here you can see session is an interface that means we cannot create object of session right but we have another way we have to search for the class which implements session again we don't have any class which implements session I mean I don't know any class which implements session so there's another way of doing this what you can do is first of all the crutch right session here session dot save now we have to create object of session right for that we need to create object of session factory now there is something like a factory design pattern where let's say if you want to buy a food where do you buy a phone in a sharp right and where that phone is built or created is it is inside the factory right so that is such an Factory so in succession in factory s F equal to again we have a twist we have to input the package first that's done if I go to such an factory documentation again is the interface that means we cannot create object of session factory okay yeah before going for a bit before going ahead I would like to tell you the founder of hibernate which is Gavin King you can see that he is the founder of hibernate here let's get back now we cannot get object of session factory right now how do we do that so again we have to create one more object here which is of configuration because configuration class it's a class ok finally not that package I have to import the package with hyper net which is hodgiedot hibernate dot G of G dot configuration so if I go to configuration finally we got a class ok no more so no more the interfaces right now once we got this once we got configuration this great object of it will say configuration or configuration corn equal to new configuration ok and now once we got configuration corn equal to new configuration will say corn dot ok so it is Bill station factory now once we got the object of Bill session factory ok we you can see we are getting a line here which is which simply means that this method is deprecated ok that means we we have to use some other method but just for time we will use this method in the next video we'll go for some other way now we will say si dot open session because open session is a method which will give you object of session now once we got that now everything is in place and that's perfect right now you can see there is no error and we are expecting that this should be stored inside the database let's try if it's on this code we got an error it says cannot connect with being null when hibernate Danny not set hold on what is asking for what is dynamic now the problem is your hibernate is is asking you here you have not provided the properties what properties they want so first your hibernate wants to know which DBMS to connect with because it may happen that in your machine you have multiple DBMS you have mask you have Oracle which DBM is to connect with second in your database let's say when if you specify masculine masculine be having multiple databases wide you have to also specify database you have to specify the username password as well and we have not provided any of those now how do we do that that will be saying in the next video right so there's some suspense how it just will work that we see in the next video so before going for the next video we have to make sure that we we have a hybrid net plug-in in your system and your necklace I especially now why we need hibernate plug-in is because eclipses provide you some features using which you can create the configuration for hibernate okay the next one will be the Koala hibernate thing so what you have to do is go to your help menu in your menu bar and go to ok so Eclipse marketplace and this you have to search for hibernate or luckily I have a half that I tool installed ok it will take some time and let's search for hibernate here we'll say hibernate and you can see you will get an option now ok sticking out in some time so now if I if I scroll here and you can see there is something called as J bar stools again this this has hibernate and you just have to install this while installing it will ask you for the available options you can see it has mavin it has mobile devices JSA we don't have to take everything you have to only check you have to only only go for Hyper night if you've done that you will get the hibernate plug in your system so again you have to sir you have to install this plug-in which is JBoss tools not all the tools when you when you're installing it it will it will ask you for the number of available tools you have to check the hibernate uncheck everything else and say finish that's where you will get your hibernate in your Eclipse and now let's see the output once again if I on this code you can see we were getting an error all right and that error is it says hibernate dog dialect not said so the problem is we have not specified the configuration right because when you want to connect with database we have to provide certain certain details right we have to provide the DBMS which you are working with you have to provide it it have its name you have to provide user name and password now how do we provide that this is one way you can do that with the help of XML file now how to do that let's right click on your project and we say new and if you click on others you can see we if you search for hibernate it will give you a file which is hibernate dot C of G dot XML so that file name is hibernate dot C F G dot XML will click on next it will ask you for certain things the first one is the dialect so if you are working with MySQL I'll be selecting mask you'll here and if you're siding with Oracle you have to cancel it will icon then you have your sake for your grabber class and I was acting calm dot MySQL God gives our driver not depend upon which DB which dry way you're working with so if you are working with Oracle you have to Spanish mention or what I call there if it is not then in the in this drop-down you have to type it by yourself ok nice do you have to specify the connection URL so I can I can take the template from here okay I will to place this thing with I'm using the same machine which is localhost so local host then you just specify the port number which is 3:07 and then we have to specify the database name and that is database name is neon here okay and then we specify the username I don't have a password from SQL so I'm not mentioning that but if you are if you have a password from a skewer you have to specify that as well if I click on finish now and you can see we caught our CFG file which is hibernate dot CFG file with all this configuration and that's why I love IDE is very very will get all this thing by default okay and that's it we have specified our configuration file here let's go back to an application and if I don this code now and you and other it says connection cannot be null I'll get is saying it is not set the problem is even if you say configuration chronicled a new configuration it is not actually going to this file even if you have this file now how to mention that you have to go for that file is by saying configure now if you say configure it will use that file okay and in this country got you can also specify the the name of the file we can specify hibernate dot C of data XML but since we are using a default name then we don't have to mention that but let's say if you want to change the name if instead of having hibernate dot C of G dot X emulation of that you want may be hibernate XML then you have to mention that file name here now let's run this and okay now we got a different error or not the same as at this time we are getting a different error it says unknown entity which is farm dot the disk or dot demo hype dot alien oh it's not an entity I don't know let's verify the problem is we are trying to save an object right which is of type alien in order to do that we need to specify that this class is allowed to store it's the to store its object inside the database okay so we have to use certain annotations to do that we can use a trait entity we can second row space and you can see it belongs to a packet which is Java X dot persistence so this is basically JPA which is Java persistence API now once you have mentioned the entity let's go back to an app and let's run this now and you can see now we got another or the same error it says unknown entity now even if you specify the annotation there while still not working X because your build factory or when you create this configuration you have to also specify which classes you are working with safe display we have to say add analytic class in this you have to mention the class name de to say alien dot class let me repeat to specify that you are walking with alien and that's an entity you have to specify at a data class because we are using annotations right so on this once again and yes now we got another error it says no identifier specified now as the what is the issue now the problem is every table in hibernate should have a primary key right in this table if I if you creating this table for alien you do not have a primary key mentioned right so I have to mention the primary key as well now do you mention the primary key it's very simple we have to use one annotation called as @ ID and if you say control space again you have to input the package once you do that the package is the package is important and you've got an annotation as @ ID which specifies that you are getting a primary key let's run this once again now and okay something has happened I don't know what what went right or what went wrong let's go to MySQL and let's see if you got that table okay and let's say show tables let's verify how many tables we have now and run record nothing but don't we have we have not got any alien' table that means we have to create that or not exactly however it says don't worry I will give you the table but we don't we have no we are not getting the table what's the issue there the problem so one of the problem is whenever you say you want to save the data into database you are making some changes right and whenever you make changes you have to follow the acid properties now acid property means atomicity consistency isolation and durability so to maintain that you have to make sure that all the - all these changes should be a part of the transaction now how do we do that it's very simple create an object of transaction so that you are a part of transaction so we say transition from rajid not hibernate will say TX equal to new session dot begin transaction okay at the end after saving have to say TX dodge comment okay so we are study the transaction and then we are committing it now let's run this I hope it will be done now and oh we got an error it says table neon dot alien does not exist now what is that problem now the TV is not getting created that means you have to create a table no what we'll do is we have to specify in your CFG file that I don't have a table create a table for me now how to do that that we'll see in the next video thank you so much and do subscribe and like this video so now let's see how to create that table so in your mask well you can see we have not caught that table wide even if I even have to running this it's because I mean you have two ways now one is you can create that table by yourself or you can ask I want to do it now the way you can do that I mean the way you can ask highway to do that is by specifying a property so you can say property name as HB m to DD l dot Auto okay again you can use control space to get that and you can specify update so when you say update in fact we have update we have create so when you say create it means whenever I run this application it will create the database it will get the tables for you so for all the entity classes it will create tables for you if you say update if you if you if if the team is not that they don't it will create ok it will not replace your existing table so now let's see if it is working or not and if you want this code and ok so you can see we got our table not found alien but after some time we see it is saying that schema update complete let's verify let's go to your mask you'll if I say show tables now you can see we got a table here which is aliens let's verify do we have a data there so it says select star from alien and I fire on this quote you can see we got the data as 1 0 1 naveen green so finally after this four videos all in the fourth video we caught our output right so we are able to run run this application in which we are getting the output in the the database so that's how you store the data inside a database using hyper tonight ok but there are you know the we also getting our before before going ahead or you can just have a round of applause for yourself for computing this code here again now what I will do is I will create so see if you can see we are getting a method which is deprecated right so what happened in the latest watch and it just wasn't out the hibernate which is after 4.1 we got a new class or new interface which you have which we need to use to work with the session factory now what is that method so this interface called as service registry or not of this package with a service registry it belongs to package watch it out hibernate dot service in fact hybrid it provides lots of services one of the service which you have to use is service registry we'll say our eg equal to new service ok so now we have to use service registry builder because that's that's how you can create an object will say service registry ways that registry builder and then we can say dot apply setting so you have to up like the settings of the corner object so it's not setting it is settings you have to remove that settings and then in this you have to pass the object I mean you have to pass the properties now how do we do that we say count dot get properties because all the configuration is there inside this count in this inside this corner object right so we say properties dot then you have to say build build service registry by doing this you are getting the object of registry or case you have to say new service build service registry builder apply settings and then you have to say build service registry now once we got the object of registry we can pass that here we can say our eg and now you can see the deprecated method is not there so it can be a car to build such a factory and now it should work if I love this code and we got error now what is this error you can see it says to make it entry 1 0 1 it's because we already have one zone in our table right so if I have to say 1 0 2 this time let's change our alien name we'll say this is medulla and let's say we have a color here which is blue so weather-wise in blue color let's run this and you can see we have ok so we got our schema update let's go to database to verify file on this code you can say we caught two entries now once you're to Mozilla and blue so that's how you use hibernate to store data in your database there are lots lot of lot of concepts remaining in this so that will be saying in the subsequent tutorials in the last video what we have seen as we have created dinner we have created this application where when you run this code it will be so it will be storing this object in the database right now in the hibernate dot CFG for xml file we have mentioned update right so that's why whenever you're on this application all this new data so if I let's say if I change this to 3 and if I say hush and let's say for color hush is let's say blue right let's see on now in this color if I if I don't this code you can see in our database we will be getting hush as well if I don this code and you can see we got hush but you can see we are not creating database again and again so it is using the existing table is because we are using update but what will happen if I say create and if I change this value from 1 0 3 2 1 0 4 and with the same name harsh NC on and if I run this code if I go back to my database you can see if I run this code you can see we got a new table so it has deleted the old table it is fitting a new table and that is what it does when you say create it will create a table every time will on this podium now what is happening behind the scene I mean when you say when you on this code see we are able to store data into database right so that means your mask you'll only work with SQL so somewhere your hibernate is converting your code into SQL at how for that what we can do is we can use one more property here and we can name this properties as show underscore SQL and we can say true because by default you cannot see the SQL query if you do if you do this if you say show ask you a show SQL true it will make sure that you you will get your query in the console window let's try it out once again if I have this code since we are using create it will drop the old table and you can see record the entire SQL query here it says drop table if any or if exists alien create a table with all this fails and it is saying not null and then we also specifying the primary key at the end travel key a ID and then we are as inserting the values right so that means even if you are not writing SQL queries hibernate is creating in SQL query for you awesome right so if I go back here and so that is we have create and then we have true so these are two two properties we can use in fact let's say if I if I say update here it will not create a new table now if I say update if I change this to let's say 1 0 1 with name naveen and if I say color is green now in this scenario if I on this code you can see we are getting the SQL query but only of insert no create and no no drop right if I go into my database now so it's always better to use update you can see it is not getting a new table every time we run this and then we don't want that right we don't want every time you know in this application it should rate it will it should raid your database again so that's that's how you use these two properties okay in the next video we are going to talk about different annotations which we can use now what we'll do in this video is we'll talk about some more annotations which we can use in this class which is alien now what more editions we can use now you see we if you go back and if you say create so that you know I want to mix I will do some experiment here and so the date the table name which you are getting is alien right but let's say I don't want to have a table named as alien I want to have a table name as something else what we can do is we can say name equal to in the entity itself and to the annotation itself we can say alien okay all alien alien table so if you want a table name like this you can say alien and discord table and if you have this code now you can see it will okay so you can see it says create a table alien table now because this time you are specifying what table you table name you want because if you don't specify the default table name would be in itself now what is happening here is of course again we can check that when you go here you can say for this code you can see record this table which is alien table okay but now what happens is many ways you run this code there's something called as a layer of entity okay so there are there are three different layers here one is your class name second is your entity name and third is your table name so we have three things class name which is this entity name and table name by the form in your class name is alien when you say your entity name is alien table so your table name which is there is a database will be will be with the ID name okay but let's say if I don't want to change the entity name I just want to change all the table name we have to use at table again we'll differentiate between the table name and entity name later once we start with hql but let's say if you want to change only your table name we can say table name equal to let's say alien underscore table it will only change the table they're not there it a name so entity and table are two different things it's just that table name is derived from a to the name when you run this code okay now so that's one of the thing we can use the other thing is let's say I don't want this this this column name is color I want the column name to be alien color so what we can do is we can use one of the annotation called as column add column which is from Java positions and M this you can specify the name we can say name equal to let's say alien maybe alien underscore name so you can do that so if you're on this code now you can see your column name which is color will become alien color okay can you see that we got not this one oh I would say the alien name I'm in my bad it should be alien color okay and if I have this code now it should be alien kalila and here we go yes so we can see we got alien color right so that's how you can specify the color yo oh god you can specify the column name to be specific so that's one of the annotation which we can use there's one more annotation which we can normally use is let's say I don't want to store the color I don't want to store or maybe I just I don't want to store the name in the table okay what you can also do that because if you see the database now and if I on this query you can see we have or not alien but alien table that is the table name we have if I am this you can see we are getting the alien color we also getting name I don't want this name to be here what you can do is you can mention name as at temporal or not temporal at transient because if you use transient here now this will not be getting stored ok file on this code okay you can see it will say it well it is dropping the old table and the new table will only have a ID and alien color we don't have alien name there let me verify that if we run this query you can see we only have a ID at color we don't have name there right so we can use at transient so transient is used to store the temporary data I mean that will not be getting stored now what else we can use so there again we have some orientation we can we can use that so that's it from this video we have we have talked about at table annotation we have talked about at column we have talked about a transient in the next video we'll see how to fetch data we have talked about how to save data right let's see how to see how to face data in this video we'll talk about how to fetch the data from the hibernate or using hibernate Tron database so in the last video we have seen how to add values right so we can use this method which is save using which you can store the object but let's say I want to fetch the data now but before fetching we should have some data right so I already have this data in my database if you can see in my mask you we already have 1 0 1 and green oh we are not using name is because we have made it transient it must run this once again so that we'll get to a transient as object as well let's go back to our database and if I say sure you can see record that record now what I want is I want to add some more values or the problem is if I say 2 and if I change this value to let's say Sarang and if I say the color is white the problem is since we are saying create every time it will get a new table right we don't want that we want it to use the same table so we have to make it update alright so we'll say ok update now by saying update what you are doing is it will not get a new table now so if I have this code you can see in your database your database will be having 2 rows now one naveen green and one 0 to certain white now if I if I go back here ok now whatever what I want is I don't have this values here ok let me just remove this values in the same code and in alien I also want a one more method here which is to string so that you know when I print the object it should not print the hash but I want to print the values of it so we will implement two string method yeah like you're not familiar with to string model you can just watch my video on to string on YouTube so to string you have to overwrite two strings so that you can fetch the values because by default to string method fetches the hash code now let's go back here and yeah so out of this what I'm going to do is I want to print the value of the disco so I will say system dot out dot println I will print the object any score lets me not save that because we want to fetch it from database now since you can you can see I have created this object it should now so this object here is a blank object I mean it's an object which doesn't have any any default values and if you try to print that object it will give you some beer output which is zero zero I have then null value I guess so you can see we got zero and then null for name and color I want to fetch this record from database now how do we do that it's very simple we have to say that I want object of the disco equal to now there's a the we can use session dot so there's a method but as yet in this case you have to pass two parameters the first one is the class name the class name is alien dot class then you have to mention the primary key which object you want to fetch I want to fetch 1 0 1 because 1 0 1 is actually in the wind right and we have to typecast it with alien because it will give you so get method gives you the object of object I want object of alien so every typecast enjoyed so once we are done with the typecasting let's run this code if you can see the output now we got alien 1 0 1 Lavigne and green so we are fetching the data from database in the backend you can see we are getting an SQL query so even if you are saying get it is filing a query by passing a where Clause when you mention that it the ID is question mark which is actually 1 0 1 in our case if I do it for once here - of course you will be getting the data which is which is which is sorry you can see once your to Sarang and white so that's how you can fetch the data from the database so again we have two methods to use here what is get it's against load I can we'll talk about loading one of the video in the subsequent tutorials but you can use get to French the data ok so that's how you fetch data data from database you're saying I use using hibernate and we have to do that with the help of get method or one more thing or you don't have to create object here we can also send null because it is it will be giving you the object right the instance you don't have to do that and if I if I run this code now you can say it still works so we don't have to create the object you can simply specify and all you want that will do so that's it in this video what we'll do is let's try to understand how to use embeddable objects or embeddable of fields what we'll do for that is let me just remove all extra things here this one okay and okay so okay so let's remove that and let me add one more property here and we'll say that property is or maybe a name a name okay so we have a name we have a name here right but what happens you know in the in your name if I want to take a full name okay example if you talk about my name everyone thinks that my name is 720 but not exactly today let me reveal my full name on on you on YouTube so my full name is Hardy Polly I mean come on ready Bob ready okay that's weird I know so that's my that's my that's my surname this is my first name and this is my father's name okay so this is my surname this is my first name and this is my father's name okay so we have I have this big name here so it's not even the knowing commodity poverty and it just close that now that's enough for my my name so what we have is when you talk about the name we have these three things right we have first name last name and middle name what I want is instead of having one field I won't leave fields so that you know when I when I go with name it should be easier for me to work with right example if I want to see only the first name of the people I can use the first name if I want to see the last time of the people I can use last name and if I want to see the middle name I can see the middle name right what do we do for that is so we can create an array of string but instead of that we can create a class for less name okay name class so maybe say alien name class and this alien name class will have certain variables so we'll have a variable like gravich string F name then private string L name and then private string em name which is middle name because let me have these three things and of course for this we also need we also need getters and setters we'll say those skitters and setters okay let's select all these three and click on ok.now Adele's like that we also need a two string method for that also will use souls and say two string method and we'll select all these three okay now we got the two string method I can get a set up let's go back to my so this is my alien name class in this alien instead of having string now I will go for alien name nothing advantages this alien a name will have three things a first name last name and middle name of course we have to change the characters as well right since we are changing that so we'll say Souls get those and say toes I will say for a name okay now once we got this once we got this a name L name you have to go back to app as well now since we want to we want to save the value we'll say new alien again because in the last video we have talked about fetching that's why we have done fetching here right but this time we'll go for again save I will say session dot save and I want to save what I'm planning to save telesco here okay now what else we need here I want to set the values now for sure when you say set the values I will say that is scope dot set a idea I will say this is 1 1 0 and or maybe 1 0 1 because since I'm changing the structure so we have to make sure we are use create here not update let's go back to alien or not comedian app and it's set the what we can set us we can say set color I will say color is green but if you say set name that is something which is different right when you say set a name now this is not a simple string right it's an object that means to set the value first you have to create the object I will say alien name I will say a equal to new alien name and we'll send devalues will say a and dot set F name which is Naveen Kumar Reddy and then we'll say a and dot set a limb which is tardy pulley okay in fact I should change my tennis I should change my you know swap ID I should change my channel name to this one okay so once we have said all these things that sect a and here that and that's your am right so we are creating an object of name and then we are passing that name here that sounds cool right now the D in question is when I say save in the earliest scenario we were having name as a string right so when you save name as a string it will take a string but this time your name is not a string it's an object how many fields it will create since we have two classes now not one we have alien and alien name will it create two table will it fetch will it keep all this values in the same table now say to create table we have to make sure that we use entity right and for a name we have not mentioning anything let's see what happens if I run this code if I on this code you can see it will give you an error it says it is not able to find the columns for name it's because it is getting confused is it an entity or something else we have to say hey don't create a new table for me what I will do is I will put this values inside the same table so that table will have multiple values and you can do that with the help of M variable so when you say my table that means this name will get embedded inside a alien table itself so this alien table will have alien it will have a ID it will have color it will also have a name but not a name exactly it will have these three things F name L name and M name let me run this to see you to show you the output if I on this code you can see it is working see the table structure here it is a ID then we got F name that we got L name then we got em name then we got color and so then we got color and that's how you are inserting a value here let's see how this this how this looks inside a table if I'd on this corner you can see this is your sequence record a I D we got F name and lame M name and color the thing is you can see that the sequence of us okay so that's how you twelve so you can see we got once you know one record Naveen regard my surname record my middle name and we got our color so that's how you use this embeddable annotation to M'Baye your your your object inside another table of course when we use entity here instead of using my dip then it will give a new table right but we don't want a new table here and that's why we'll be using embeddable so that's it that's it from this video in which you have used embeddable annotation again we can we can change the column name here as well we can say add column we can we can do all those extra things now say this is not entity we will not it will not be using @ ID because an idea is for this is for the primary key right and since you are you are inserting this in a new table so we don't have at the existing table that we have to do we do have to do that and in this video we'll talk about hibernate relations now you if you are coming from the data base background you know what I'm talking about right so we have different relations to work with right so we have one two one we have one to many we have made too many right so one cable can have its record in the so let's imagine we have employees and we have department table so normally what we do is so one I mean one department will have multiple employees so that means they have one department multiple employees which means one-to-many and if you look at from the employees perspective it is many employees work for same department that is for this many to one right then we have laptops so I own one laptop so which means one to one and one laptop can belongs to only one person right so there's one-to-one but but let's say if you go to a library where you have buy multiple you have lots of books so the same book can be I mean so multiple people can use the same book I mean they can you know in rotation basis or one person can take multiple books because multiple people can work with multiple books that is called as made of many right so you know all this stuff come from data from the database world right so that's not the whole topic here though the whole thing is how can you implement those things in hyper night because that we can do in SQL right and we are expert in that we can create all those primary key foreign key stuff in in SQL how about hibernate let's try to do that here so you can see we have two examples here the first one is we have a class laptop which has this two records we have Li D and L name and then we have we have student class here which has roll number name and marks and these are two different entities right and you can see I'm using a notation which is identity and identity which means we will be getting two separate tables altogether one will be laptop and one will be student right so let's imagine this is those tables so we have laptop table where we have Li D and we have L name then we have student table where we have an old number name and marks which is your columns here and again we have roll number as the primary key and then we have your Li D as your primary K so this column and this column are your primary keys right but let's say now I want to create a relationship so a laptop so let's say student will have a laptop object I mean can we do that and of course we can do that right so imagine this is the class and I can be have some Gators inside naazia the way we normally do so on both the end we have getters and setters now let's say in this student class if I am creating object of laptop because every student will have a laptop right so what we'll do is we'll say private and that will be laptop and then we'll be having laptop okay by any mean if you are thinking that we have a different font altogether so this thing is written by someone else and I am writing this thumb to this stuff here so you will find it a different fonts there so now what I'm doing is I'm creating one more object here which is laptop right so we have a laptop object here which is which is inside your student class now how will it impact your table so now since we have one more column there what we'll be doing again you guessed it right again we have to create a foreign key and the primary kinship here so this class will have one more I mean this table will have one more column here and that will be your L ID right so you'll be having a laptop here let's say let's imagine we have laptop one and then laptop name is Dell and then we have row number one well let's say this is one zero one row number one name is let's say Naveen and then I'm saying marks equal to 50 and then an ID which is your one zero one which specifies that we have a relationship between this laptop and a student where this is your foreign key from your laptop table right so so yes that's how you can relate to that now already has a has a laptop which is Dell laptop right so that's how you can create one-to-one but will it work directly not exactly to achieve this someone here about your object declaration you have to mention and annotations which is called as at one-to-one okay this this o is capital here so you have to use an annotation called as one-to-one so one student will have only one laptop so student table will get affected and you have to mention that Li there simple right so when you have one object one laptop object in student table that's how you do it now let's say I don't want to use one laptop so in fact as a student or as a professional I have multiple laptops let's say I have one del 1 HP laptop and one MacBook so if you have three laptops here I cannot use one object here right I have to use a list of laptops so what I will do now is instead of having a normal laptop entry here let me go for least of laptops again we have okay that's your paint marks but let's say if I create private and we say list of laptop and then we'll say laptops again when you change all these things you do have to change the getters and setters again we'll see that in the--in the practical how do we do that but time and what you are focusing is those photoshop so when you have one student with multiple laptops and for this scenario you have to use an annotation called as one-to-many because one student will have multiple laptops right so you have to use annotation which is one too many again with without any space so it is act one-to-many that means one student will have many laptops but in this scenario how will you update your tables because say this will not work because one student will have multiple laptops let's say we have laptop 1:02 here which is HP and nubbin 20 buys I mean has both the laptops how can you how can you mention that here you cannot say 1 0 2 comma 1 0 I mean once you know 1 comma 0 2 right and you can't even have two records here because zone number is a primary key so you cannot say I mean 51 zero because none of us a primary key so it will give you error so in this situation what we normally do is let's not change this table so this table will be independent so student table will have its own records and laptop will have its own record there will be one more table which will be called as student under schoo laptop so there will be one more table for a student as for laptop in this table you will be having two columns the first column would be student or in fact roll number because that's your primary key so it will be having the old number and the second column would be l ID now the entries would be it is 1 then 1 0 1 then 1 1 0 2 let's say if you have one more showing here which is row number 2 and his name is let's say Aarthi and then the marks is let's say 56 and then I'm using ones your 3 here which is Mac Book which is let's say Mac Book okay and let's say our thing has only one laptop here which is mac book so what we'll do is we'll say 2 and once you're clean so you can see we have a table here which is used for the mapping so this is a different table called as student laptop which has a which which has all the relationship entries right because we cannot do that here but hold on we have a solution for that right instead of creating a new table what ever you can just change your laptop table I mean something like this what if somewhere here I don't have a place to do that let's say I'm just creating a column called a wheel it should be at the end but let's say since we don't have a place I've created here can I create a whole number column here so this is your primary key ok can I create order number column here and every lock laptop will be defined so this this laptop belongs to so number one this laptop belongs to the move on this laptop number two simple right so instead of having the entry in the another another table we can have the entry in laptop itself but hold on how it will work because student table knows that it has laptop entry your laptop class don't know that it is rated to student how will this column will be created so you have to mention in your laptop class that every laptop belongs to a student so for that I'll be using private student and then we'll say let's say stood again if you don't do this if you don't write this the the output would be laptop table without this column student table and then student underscore laptop table but if you do this if you write private student I mean by which to in stood now this class will know that we have a column or we have a field here of which you have to connect a column right but again we are fetching the whole number right so this should know that we are going for a relationship and the way you can do that is with the help of act many-to-one annotation so when we use activate of an annotation which means that this this laptop class is responsible to create a column from the student sorry from the student table and that will be role number right so in this student you have to mention one too many you have to mention that and in laptop you have to mention may two one now if you if you do this if you run this code the expected output is laptop table with this column student table with this field they will not be any extra tables right but hold on it will not work you will be getting extra table as well even if you don't want that your student shouldn't will say hey you asked me to plate one-to-many it is my job to create that table laptop will say you have mentioned many to one it is my job to add this column why both are doing that again ego problem may be so you have to mention to your student class that the mapping is done by laptop so you don't have to do it now how can you mention that you can mention here mapped by property so you can say mapped by and in double quotes you have to mention the name of the field which is stood so if you right studio you're mentioning to your laptop I mean you mentioned to your student table or student class that I'm doing one to money that's perfect but the mapping is done by laptop don't do it by yourself that means if you do this it will not create an extra table it will go following two tables laptop and student and you'll be having the entry at the end of the city and the end of this table which is roll number okay so that's how you use one-to-many one-to-one one-to-many many-to-one but how about many to many how we can do that let's say so we will having once joined we'll have multiple laptops and then laptop will have one student but what if you have many to many relationship so one student can use multiple laptops and multiple laptop can belong to one student that's weird right so multiple laptop can be used by multiple students so we have many to many relationship here to achieve that instead of using stood or student wait to create a list of student so we say list of student and I said this is stood since our you start with say stores now this time we have laptops which has multiple students and we have student which has multiple laptops so we cannot use one-to-many Omega 1 here we need to use many too many and here as well you have to mention many too many now again if you like many too many and many to many without this mapped by both the classes or both the tables will be having their subordinate table so if you don't write mapped by what it will do is laptop will create laptop table students will create student table but laptop will also create laptop underscore student table and student will create student underscore laptop table so in total they're getting 4 tables we don't go on that right the mapping should be done with only one table now this will not be up this will be applicable because one laptop can have multiple students so we cannot do that so we need to use a mapping table here which is which will be called as let's say laptop underscore student right and you can achieve that with the help of mapped by property one attribute so that's how you use one-to-many many-to-one may to mind all the stuff so in the next video we'll be talking about the practical information of it in this video we'll talk about hibernate relations but this will be a practical in the last session we have talked about the theory of it so for that what you are what I have done here is I have created two classes one is laptop in laptop I have ID as L ID and then we have L name and for this we have getter and setter then we have student where you have you can see we have okay so we have this student class here and then in our app class what we have done is we have created this laptop object and this student object and I want to store both the objects into database and of course you have to make sure that you have annotated both the classes here which is student and laptop and then you can simply add it can simply save the gate vanity database you can see we have save laptop and save the student now what what what what something everything is perfect here let's run this so if you run this of course there will be a table for laptop and there will be a table for student and you can see I'm not using any specific name for this or tables they will use student and laptop table as I mean only so you can see we have an ID L name and then we have marks name and roll number now if I go to the go to the mask ul and you can see you cannot see so I have this neon as my database and you can see we have two tables here which is student and show tables we do have two tables your laptop and student and if I say select star from student will be getting details for this student which is 115 Naveen and if I say laptop you can see the Koch one to the 1 and del again the one waiting for student and laptop because in the theory we have talked about these two classes how can you can continue the same example for alien class as well which we which we are doing from last few videos okay so what what what's new here so what I want is there should be a link between a student our laptop because every student will have a laptop right so what I will do here as I will say private let create object of laptop here I will name it as laptop thing and then for this property we have to use getter and setter of course so we have to say right leg will say source which is here and we'll select getters and setters so we'll select this part and the click on okay okay so you can see we got Guinness stuff for that top as well now so student class has a property which is laptop so whatever I mean see we have a number we have name we have marks and we also have laptop so it should have a laptop property right but then laptop is not on normal variable it's a object so it will go to the laptop class and see okay in laptop class we have L ID as the primary key so in student table will be having L ID as the foreign key right let's try let's see or before that we have to change in app as well we have to also set the laptop here right so we'll say as dot set laptop and we'll say laptop laptop itself so you can see we have laptop here in fact in Eclipse I'm missing this thing in fact if I if I want to change this student it should change the all the variables here right I've seen that in some of the idea is I believe but even you type so if you select this s and if you say if you type student all this reference should also change but I don't know how how to get that if you know let me know in the comment section which ID he's supposed that okay let's continue so what I'm doing here is I'm setting this laptop to show into as well and I fire on this code okay we got an error because because yes so what what's wrong here is it is not able to define the type of laptop here so whenever you create object inside student you have to specify what is the relationship between this laptop and a student for that you have to use an annotation which is called as one-to-one because one student will have one laptop now let's try if you on this code you can see it worked so once 20 will have one laptop that is your one two so that's your one-to-one relationship now if I say show tables you can see we got laptop and student this is this this is our old tables just ignore that we have laptop and student and if I say it's an X term topic will be only a laptop entry because laptop don't know about the student but student knows about a table so he knows about the laptop right because in student class we have updated with the laptop object that's one to one to one but let's see how to implement one-to-many here for one-to-many whatever doers let's say one student has very laptop so of course you cannot use normal laptop here we have to say list of laptops will sell list and then let's create object here itself will say new added list ok let's import the package for both will say Java dot util okay so we have imported that okay it's not getting imported for this award oh ok so since I'm using I have set my project to 1.6 and that's why it is not supporting this diamond here you have to specify that this feature of not mentioning was introduced in 1.7 so of course you have to mention here because my JDK ESSEC to 1.6 okay so instead of using one-to-one I should be using 1 2 may now right and for this you have to input the package let's do that now what will change is we have to change the getter and setter as well so instead of saying get laptop will say return the list of laptop and here will mention list of laptop as well ok now in app you show up setting the laptop here we'll say s dot get laptop dot add you have to add this laptop Dell right so you have to add this into added East so the changes what we have done as in this student class we have mentioned about one-to-many and we have created the list of laptops not one laptop you have lists of laptops and in this app what you are doing is you are saying get laptop which will give you the analyst and then you can say add I hope it will work it will refer on this code ok you can see we got an error is because it is not able to delete the table ok this is important because sometime it because of dependency it is not able to delete that the problem here is it is no delete the laptop table because our student table because student table has the primary key of laptop right so you have to delete this part so if I first have to delete the laptop comfortability student here so that it will create a new table and it is it cannot delete because okay we've already got so you can see record the third table let's do it one by one you know this is what you have to do if any one you create when you will run you have to make sure that I did it all this extra stuff so we'll set it delete the laptop and let's drop drop and drop well let's run this let's go to eclipse and say it one there should not be an issue now and you can see we got the output but the change here is if you see the output you can see we got laptop table that makes sense you got sewing table that makes sense why do we have shown in laptop because we cannot do mapping in student table now because one student will have multiple laptops so you cannot do that entry into student that's what it is it has created a mapping table where you have mapping between student and a laptop and that's what the name is student laptop right and if I say so next are that if I say select star from was it will do that let's say select star from student laptop and if I say father's query you can see we can be gone rule number one got laptop 1 0 1 and if I say student table they don't know about the laptop and if you said not talk to you what they don't know about they stood it right that's that's awesome right so that's how you are one to many works but so now I think laptops should know about this student as well because if laptop knows about the student and one laptop I mean one laptop will belong to one student only right so if you can create this student reference in laptop as student and for that of course we need gate etc let's do that we'll say write like source and we'll select getter and setter but this time it will be for student will click on ok because I was trained there but this student should know what mapping they're doing so far student we have multiple laptops right so in student we have student has multiple laptops that's what is one-to-many because one student will have multiple laptops but in case of laptop it is multiple laptop belong to one student so we have to use many to many to one now why do one is because multiple our table will belong to one student so with this the mapping will be done inside laptop table we don't need extra table to map that okay but before running this thing again that's the best thing is drop all the tables so that you know it will not get any sugar running will drop student and will that will drop laptop as well now if I run this code okay so you can see it has created the laptop laptop table that makes sense because it has la villita has student role Amma can you see that now this laptop table knows about this student this laptop belonged to watch student right and then we got student which doesn't know about the laptop because we have a mapping table but when you when you have this entry in the laptop table why you need a mapping table okay the same problem laptop thinks that his is possible to map student thinks that he is possible to map we have to mention we have to mention here H student you don't have to map it because this will be mapped by and then you have to mention that it is mapped by this variable here which is student so you don't have to do it I would simply specify student so that so this time your student class will not map it but again before running this I have to draw all the tables let's drop this let's drop this one and let's drop this one as well oh it is not able to delete this student table because there was a laptop table and let's tell it that is it working now oh it's a unknown table new your lab showing that table is it the table name I know what's right what's wrong there let's run this thing now once again and can you see that this time we are not getting a new table it's because the mapping is done by laptop so showing that don't have to care about that right so that's how we use made one so that's one to many many to one the last part let's say this laptop has list of students here so we don't have one student we have I mean we don't have a bunch to it so multiple student will have used multiple laptops like you know let's say if you have 10 laptops in a in a lab in a college lab a computer lab and then any student can use any laptop so that's many too many too many right let's import the package for this let's say control shift oh and we'll say this is a what's that will say new array placed and this time we'll mention student ok we didn't put the package for this as well so we're saying is right and then you show up saying made what made to one we have to say may too many way to input package for that as well and he always returned the list and while setting also we have to set the list not once to him but at least top students right so that's done we have created this laptop where we're mapping is done with the student but here in app every change something so laptop should know about the student as well how do we do that we'll say laptop dot set such student then we have to say set student or not just said so it should be get student get student dot add and you have to mention student but that should be done afterwards somewhere here because once you have created this to then then only couldn't you can specify that right so now student has added laptop and laptop is adding student so we should work now we can see we have these table students we still lists off laptop but you have to make sure that you write may too many and here also you have to mention many too many not one to many and let's see if I if I not if I'm not writing this mapped by on this let's input the package for this okay so what I'm doing is in list we are mentioning many to many so One Laptop block can belong to multiple students and one showing you can have multiple laptops before running this of course we have to do that stuff let's drop this table anyway don't we don't have that table now and let's drop this to table okay now if I say show tables you can see we have only a lien there is no footprint of student and laptop let's run this let's see what happens if I miss code now you can see it worked but how many tables we have we have laptop table that makes sense to in table that makes sense laptop shouldn't also make sense but why we have shred laptop when mapping is done by laptop why shouldn't us do that it's because laptop thanks ok is this possible to map it certain things that is responsible to map it let's specify the student hey you don't have to worry about that this is done by the laptop so why you are mapping it so let's say let's again do the same thing mapped by student okay let's drop all the tables because if I say show table so you can say we got four extra tables there let's drop all this table will say drop table laptop under school student okay let's drop this one let's drop this one let's drop this one and all the tables are dropped now if I on this thing okay and you can see the number of tables we got laptop we got student and the got laptop should and now we don't have anything called as student laptop and if I go to my ask you just to see how many fees we got let's remove let's say this is laptop student this far the query for laptop we got laptop entry for the query for student record student entry and if I say laptops showing to be called the mapping as well again one laptop can have much because students and multiple are talking high whilst one sure that's how it works right so that is one-to-one one-to-many many-to-one and mate Oh many so if you have any questions let me know in the comment section and that's it in this video we'll talk about the different type of fetch techniques we have eager and lazy now it's it's almost same as human behavior right so let's say if you get a task what you do you wait for the deadlines that means that that means lazy but if you are kind of person who the moment you get a task you do it that is eager now to explain this what I will do is you know just well we have some code changes here we caught a different class here so you can see we have eight in class so we all are aliens by default right so alien will have an ID or name and the alien will have multiple laptops normally you know you work with multiple laptops in one machine you'll be having one one type of task is running on the other machine you have some other tasks other tasks running so let's say one lap one alien will have multiple laptops and so in again we have a ID a name and laptop collection and in laptop we basically have we basically have Aryan it's not a different example right which we are doing from from long time so and then what I want to do is I want to fetch again we have done this before we have talked about one-to-many relationship right so let's try to fetch the value again now what I'm going to do is I will simply ask for alien because I already have a database created so if you can see I already have a alien database here or not agent database might test database in which I have alien and laptop so if i say alien you can see we call three indians we got Naveen wrong with that bank and if I say laptop so we have five laptops and in a 91 has three laptops that's creating an i3 has two laptops and unfortunately Rahul doesn't have any laptop a poor house right so so let's go back here and let's try to fetch the value so what I will do is I will say alien a1 and I want to fetch the data of alien from the database so what I will do is I was a session dot get and then you specify hey I wanted it about alien so you say in inter class and then in this you mentioned the ID oh let's see the ID is one that's I'm talking about the bean here okay so if I simply fetch the value what do you think do you we didn't get all the values and of course you should get the you should get all the values right now that means if I fire the query of course you will get the SQL there run this code and let's see what happens use on this code and you got you can see the guard and SQL and okay we're I'm also fitting hi here we're okay let you I don't want hi anymore which one this and you can see we got we just for adding a query but then it is only fetching the Illinois D alien name you can see know where it is fetching the collection instead it is not fetching the list of dots so if I try to fetch the least of laptop so i if I say let me print the value of a1 and then or maybe I will not print the value of a1 because if I okay let's let's use only the Delap top part so I will first print a1 dot get a name let's fetch that one and then I will fetch the least of I will say collection of laptops that's what I'm using right I'm using collection here so I will say connection of laptops and I will fetch all the collections here and I will say a1 dot get labs because it will give you the creation of laptop and then I will iterate with with the else before logo I will say a laptop l : labs and it will train I will try to print one by one laptop I just want to know was it very far the query now because you can see laptops and subject table right why it is not fetching it and I'm going to try to press the value you can see we are filing the query for laptop as well so when we when we were saying that we want to fetch alien it was only filing the query for alien but not for other laptops it is filing the query for laptops only when you want it so if I again come in this section so if I comment this section it will not file the query for laptops that means even if you are fetching the value of alien you are getting only the value of alien not laptops even if alien has multiple laptops that means it is lazy but I don't want the Z I want it to be eager because see what what is happening is it is filing the quality two times I mean two different intervals so first you are filing the query for alien the second time you are fighting query for laptops so I use I will feel a bit delay here now if you want to make it eager that's your choice again that's the choice of the way you design yourself but if you want to make it eager it's very simple actually in your collection you can specify hey I want to specify the fetch type so in one two men you can specify hey I want to specify the fetch type and the fetch type I want is so we say fetch type dot this option of eager by default is lazy if they're willing to make it eager and even if you are not fetching the collection for I mean even if you're not fetching the laptops here and if you run this code you can see it will fire this ql query so you can see at least it is find SQL query now this time it is fetching a ID a name and also the laptop deed I can see that in this electron it is fine the it is filing the data for laptop as well but then wait with one difference it is not filing two different queries it is finding only one query and it is using something called as left outer join is it yeah so it is using left auto join to fetch the value so let me remember this by default the fetching type is lazy so when you say lazy it means it will fetch the value only when you need it otherwise it will not give it to you it's something like let's say your boss says hey hey I mean I want you I want I want I want to complete a task I want a these are a task you have to complete now I will say okay that will be done and I will not complete the task and let's say one day my boss says hey you have to us now and do me the task at that point I will start because I'm thinking maybe my boss will not ask me for to complete the task why try to waste my time and that's what happening with hibernate maybe I use a don't want laptops then why to fetch the value that's what happens I when it is thinking so that's why it is lazy but you can you can make it eager that's very simple let's go to that I meters go to aliens and say fetch equal to fetch type eager and it's that simple yeah so there are two type of fetching value fetching techniques we have eager and easy and that's how you do it by default just lazy you can make it eager and it will work I hope you enjoyed this video you understood about fetch and I mean eager and lazy and in this video is talk about hi caching now if you're thinking that what is the advantage of using hibernate instead of using JDBC there are lots of advantages right one of the advantages we have seen is o RM which is object relational mapping so is it the only reason to use hibernate not exactly in hibernate we have some more features which is one I mean one of the feature is hibernate caching now what is what is caching or cache so basically when it whatever you want to access the same data multiple times instead of hitting your database every time you can use caching right so how exactly it works so let's say we have our client here and then we have our application and then we have our database here right so whenever a client sends the request to the application server that request goes to your your database server right so let's say you have a client here and a client want to fetch the data so what we normally do is we create session object right so in hibernate we have to create session object then we have to use transactions right so when you are fetching this thing we need to use transaction which is a part of session so what happens is whenever you whenever a client signs the request to the application server that goes to database it will fetch the data let's say I'm asking for select star from students so if I'm filing this query you can imagine how the data will be getting it is not one or two records because we are not doing any filtering we are saying the next select star from student it might have 2000 records maybe 3,000 across maybe 10,000 records so in this scenario when you fetch the data it will be coming to the addiction server and the from the application server it will go to the user so that user in that particular session right but let's say if I'm again in fact I'm filing a query so let's say I'm requesting the same data for the second time from this for the same user in the same session okay so in the same session if I'm asking for the same records it should know so what it will do is it will again it will again go to database server it will again heat your database server it will fetch data time consuming right so when you have done that query for the first time it should be stored somewhere so you can I say memory it should be there so when you when you lose quality for the first time data should be there in the cache of memory so that for the second request it will check okay we have the same query let's go for the data here right and now this cache a is provided by your hibernate and this cache a will be called as first level cache it okay so for the particular session so this is one session or let's say if I'm using session 1 for that particular session we'll be having a first level cache it but let's say if a user is changing this session may be the same user is going for the second session or we have another user who is going for second session so we have two different session now now this two different sessions will be having two different first of all caching that means if s1 or in this session one if you are finding this planning you have your data in the first level cache of s1 now if s2 I mean the second session is following the same query now the data is not available in the first level cache a obsession - so again it will hit the database right so again we have the advantage is if you are filing the same quality in the inn in the same session - it provides you first of all cache it but if you have two different session then hybrid says ok you cannot use first of all cash' here then we have to go for something called as second level cache a so basically it has two levels one is level one and level two so level one is by default given by hibernate okay so even if you don't want to use it happen let's say say take this take this option of using first of all caching right I can how to do that with that well say the next video but this is how your first level cache a works we know it in the same session so let's say I want to use second level cache so what second level cache is says says is it doesn't matter how many sessions you have all these sessions and the same application can share the second level cache it right now how did it will not give you by the point okay you have to configure this so first of all you have to use some third-party library for this so again when you say third-party providers so we hide either we can use eh cachet that is one of the option or you can use Oh edge oh oh s then we can use we have one more we have swarm right so we have all this cash of enable now which is best so everyone prefer to go for eh caches so in this in this session we'll be talking about e edge caching okay now when you say e HK see how it works so we have to configure certain things let's go for the first one so let's say if I'm using e edge caching you have to configure certain feels now what feels you have to configure first of all since we are using now so let me just create a line here so first thing the first thing we have to configure is your P om dot XML file because see when you say you are using eh cached that means you need to download that library right so that library name is eh cachet you have to you have to download this cachet from the maven repository you can go to Babel deposit you can ask for an e H cachet you buggy transpose then next you have to also download the jar file which is hibernate e H cache a jar file again for this is for integration so this is the this is this caching will provide the features and this will provide the integration of e H and hibernate the next thing we need here is so you have to configure your hibernate dot C F G dot XML file in this C by default your second level cache a is disabled you have to enable it so you have to add certain properties in your hibernate dot CFG XML file which you specify that ok we want to we want to allow or we want to give the permission so that we can use second level cache it you have to configure that and hibernate dot C a0 XML so first you have to give the permission second you have to mention who is your provider so our provider in this case is education both this thing have to provide in hibernate dot CFD dot XML file the next quantification which we need is we have to change your entity because by default not every entity is cacheable so you have to provide two annotations for that the first annotation is called as ad ad cachet the second one is in fact the first one is catchable the second one is caching so by by specifying that it is cacheable that you are saying that you can use I mean this entity is eligible for caching the second one is caching because you have to flow you have to specify these strategies okay there are different so this is available we have video usage again when we start with the practical implementation you will understand what I'm talking about but we have to provide two things one is cacheable and second is the cache a strategy ok so if you can configure this thing you you are using the second they will cache it ok how exactly we have to do that as we'll see in the practical video but time pointed member is we have the concept of cashing in hibernate we have first of all cache a we have second level cache a whenever you go for first of all cash it will be in the particular session because every session will have its own first of all cash it for multiple sessions if you want to share data then we have to use second level cache now second level cache me for that we have to use certain providers we can use eh cache a we can use OS cache a we can use swamp one of the best one is eh caching so for that you have to configure your ponder XML file you have to configure your hibernate file configuration file and then you have to specify cashable on the entity ok so that's it that's how you use hibernate caching in the next video we'll talk about the title implementation act in this video we'll talk about hibernate caching in fact in the last video we have seen the theory of hibernate caching right how exactly it does caching so we have seen behind every one cache a we have never to cache a let's try to implement that here and as you know that level 1 cache a work with session so every session will have its own level 1 cache a and the moment you say you have two different sessions then your first level cache a will be different right for that we use second level cache so what I'm doing now here is we have a session with name session 1 and then we have such anyone not biggie transaction size and 1 dot you get transaction commit which will commit the transaction and all the transactions should be done here so and we are using a table you can see I'm using an table name as a lien table so if I go to my mask you'll you can say we have a lien table here let me remove this extra values here so yeah so we have a lien table here so if I fire the query you can see we have 3 records there 1 0 1 1 0 2 1 0 3 then we have 3 names now wind ready now in 120 and I mean - ready and nickel I see it is a matter so what I'll do here is I want to fetch the value so how do we fetch a value because if I try to paint a there if I say print a of course the value of a is null so it will print null value okay you can see it is null but I don't want a null value I want to fetch some values I will say a equals you will cast it with Union because we know that whenever you fetch the data from the database using hibernate it returns you the object of object so we say session 1 dot get that's the method we used to fetch the value and we'll use a class name as alien dot class that's how you specify the class name which will map with the table of course and here you have to specify the primary key so when I say 1 0 1 I know 1 0 1 is now going to ID and color is green so if I run this query now you can see the output which we got is the object which we which we wanted and it has all it has all also fired a query which is a select query right so for one fetching it has fired one query but let's say in the same session if I do this once again with a different value so I'm again fetching a value so after printing here if I'm again fetching the value but this time instead of going for 1 0 1 if I go to wash it off once you're too so of course it will fire two parties right you can see we got two values that's for sure one is now introduced against I mean now in one dot I mean 120 but the number of queries it as far as two queries become one with was it a 1-1 with ones here - but what if you fire the same query you want the values with 1 0 1 and 1 0 1 if our fathers query now you can see it has far the query only once but the output there are two objects now what is happening is since you far this pony for the first time see if any one you fired any query or whenever you work with hibernate it will first check the value in the first of all cashing and you can see this value is not dead in the first of all caches so it will go to the second level caching it's not also dead in second of all caches it will go to the go to the database it will fire the query for the second time it will check ok do we have the data in the first of all caching and yes for this value 1 0 1 if we do have value of the first of all caching so it will fetch the value from there it will fetch the value from the first of all caching right so that's how it is firing the query only once and that's how you improve the performance of your system because we are not going to database for the second time right but the moment you change your session let's say let me just end this session here or let me just do the commit ok cut paste commit let's do the commit here and let's keep this part in the second session how can we do that let's copy this code will say copy and paste ok we cannot say copy/paste we should say code reuse that's that's the awesome word right so we said session 2 and here we'll say session 2 dot begin transaction and here we'll end this session is a session or one dot close ok so we are we are going for a second session here session 2 and here we'll say session 2 dot get transaction and we'll say dot commit okay and we end the transaction as well so we'll say Session two Dodd closed now let's see how many how many times it files the query you can see we have the same data but this time we are using two different session this is one session which is getting close to your this is the second session of I've just change this resistant to so this is the second session here and now if I fire this query and you can see it is it is fine that query two times so that means it going to database for the second time as well so that means your first couple caching works only for that particular session now how do we use with the so we have to use second-level session here second level cache it to achieve to fetch the data from the second session or the second sessions we'll use it it of the first session provided your values are there in the second level cache but hold on first thing with cachet is provided by - entire fault right what about second level second level session second level cache it so second level cache a is not provided by default you have to configure it as you can see in the hibernate configuration we have not specified anywhere that I'm using second level cache okay and that's why it will not use second level cache so first you have to specify that I'm going for second level cache is somewhere here second yeah second thing you have to specify is you have to get the provider for the second level cache because I when it will not provide you by default so you have to get the external files and one of the fun which one of the caches second about cache a we can use here is e H cache it okay so first you have to download the jar wants for you you eh cache a you have to download the charge files for hibernate and EH cache integration then you have to configure your hibernate config file and then you can use a cannibal cache it seems difficult right but don't worry we'll do it give the second video how exactly we can achieve that let's continue for the second level caching so in the last video we have talked about the level 1 cache rewrite and before that we have talked about the theater of it in this video we'll talk about the second level cache so let's start with it so let's if I go to app dot Java and then we have returned to sessions right this is your first session this is your second session and we know that first of all session on first of all cached works only for that rubber session now since we are changing this changing this session that means it is not able to find any trying to cash in the first table caching so we have to implement the second level cache now how did we do that now we have to follow certain steps ok the first one is you have to download the judge files all you have to add the dependency for each cache now how to do that just go to hibernate dot and on this file go to p om dot xml file this is the this is where you mention your dependencies so if i go to p om to xml file you can see I don't have any dependency for eh cache so the go dependencies I will click on ADD and we'll search for e H cache again we can go to my main repository and we can find it but otherwise we can do it from here as well so it is e H cash from night dot SF that's the domain name okay this one so have to select e net dot SF dot eh cache and you can go for the latest version no problem click on ok you got one one dependency we have to add one more for the integration of hibernate I need cash for that will search for hibernate - e H cash and you can see we got this one here click on that and click on ok now once you have done that go to pure and XML file make sure you have those do two things here you can see we got these two dependencies we can't save and once you do that you can see in memory dependencies it will be getting added one as the eh caching it's not stitched it is still building so it is eating downloaded from the internet which will take of course which will take some time so once you have done with that once you have got a dependency the next thing you have to mention is you have to go to your hibernate dot CFG XML file and you have to mention which I mean you have to set two properties first by mentioning that you are using second level cache because if we don't mention that in your in your hybrid XML file it will not get downloaded or it will not enabled to unable that you have to set one no property will say property oh it's taking some time go in the bed lounge okay so I think I should pause my video for a time being because it is getting downloaded and okay so I got my dependencies that you can see we caught hibernate eh cachet I got E H cache right and if I go back to the PM file because of this - for to increase here we got those dependencies right now I will go to hibernate and I will set two properties first one is you have to set hibernate dot so you can see if I say control space we got an option of cache and inside this cache there is there should be an option called as cache dot use second level cache a by default this isn't this is false let's add it true because you have to use to the second level cache a I'll be done with it let's try then this is going to go to my app and let's fire this for you once again because we have done two things and you can see still it is going for two just going for two queries that means we are not done with it we have to provide one more thing now whenever you use second level cache you have to also specify which provider you are working with because we have different providers right we have eh cache we have swan cache and all the stuff since we are working with eh cache we have to mention the property which is hibernate dot cash dot so in hibernate 4.0 4.0 3.3 we have to use instead of using provider class before that we used to use provider class but now we have to use region factor because if you go to certain websites there they still use provider class because they are using hibernate 3 now since we are using hybrid for we have to use region Factory underscore class in this you have to mention the class name now which closet is so if decide or a hyper neck or G dot hibernate good insulation so I go to type it complete stuff so we say cachet then we have to say e H cash in fact we can we can copy that you have to just go to hibernate cash this it should be here so if I expand this you can see this is the class I want to use so I can copy this I can say copy qualify name and we can paste it here should have typing that stuff we can paste it when that works let's remove this extra class at the end which we don't need so this is the this is the properties have to set two properties one as the second double cache a unable and second is you have to specify the region factory name now from where you will get the effective name we have to go to hibernate cache which is our hibernate eh cash jar files you have to expand the first one and you will find your class name there now once you got that let's go to an app and say run it should work now and it's not working it says what it says hibernate oh no class definition found for which class okay let's try okay it's not able to find the definition made any mistake here so that's oh we don't have to specify Obagi you can simply say hibernate all we do we need who or watch it here and what else is remaining there okay let's fly once again it says class not found last name last for the implementation okay let me just change the version from is it the wasn't issue let's try it oh I'm using the hibernate it's my hibernate version so you can see I'm using the hybrid version 4.1 points in the version which is we are using here is different you have to make sure that put they both are save right and I have a god dad dependency is I got it and if I run this code now it should work let's try and it worked but again the problem is we caught again we got two coils now what's wrong we have we have specified and the we have got the dependencies we have specified that we want to use hibernate but the problem is not all classes or not all entities are allowed to get cached you have to spec you have to explicitly specify that you are using the cache for that you have to use one annotation called as cacheable so your class need to have an annotation called as cashable second this concept of cache a has multiple you know multiple strategies one is read-only what is none what is readwrite so what is those strategies are so let me just write to change your strategy have to say cache it because by default is none you have to say usage by I mean what way we are using that's a cache a concurrency strategy that's how you specify the strategy then dot and you can see it's an enum where you can specify different strategies here we have done which means by the quite is done it will not use any cache a concept you can specify non-strict read/write you can specify read-only literally means it will only work for reading god for writing so if you if you don't update your update your database with only is best but if you think that you normally update your database it's best to go with readwrite let's use read-only here and that's it I think we are good to go let's go to app and run and you can see we got only one select query now since even if you have two different size it is finding only one query because they both these sessions are getting shared with the second level cache it okay so let me repeat what we have done you have to first update your eh cache dependencies to dependencies one is eh cache second one is hibernating years cache you have to make sure that your the hibernate version is with the hibernate and cache version is same as they have been watchin you're working with next you have to update your hibernate dot CFG file by matching these two things which is your new second level cache a and region factory class and on your entity have to mention two annotations one is cashable virtual which will allow it to get cached and second to specify the strategy which you want which you are using so you can do that with the help of add catch a add cache annotation so that's it that's how we use this thing in fact we can do one more thing here let's say if I go to app and if I specify let's say if I don't do if I don't use this if I use a query now how can we achieve caching using query in fact what we'll do is we'll do that in the next video in this video we'll talk about hibernate caching with query so in the last video what we have done is we have used hibernate caching second level cache right this time we'll go for queries because he won't the data which we have fetched is with the help of get method right so when use get method as the hibernate implementation so that means hibernate will use cache but what if you specify your own query let's say if you are using SQL will it work let's try so before going before going forward let's do a quick division what we have done so we have added these two dependencies one is the eh cash and the hibernate and k eh cache then we have specified that I want to use I want to use this thing which is hibernate a second level cache and factory class then we in this we have mentioned that we want to use cache right in the entity Anthony class and this let's try to implement the query now how do we use query so we'll say query q1 that's q1 equal to I will say session 1 dot create query and then we'll mention a query here we'll say from alien where I will say well a ID is equal to 1 0 1 as the that's the data we want right and then let's import the package for this we'll say Oh watch it on hibernate does the package where you have your query then we'll say so here instead of fetching data like this we'll say q1 dot unique results that's how you press the value so if you if you know that you will be getting only one row you can use a unique result and we can cast it here we'll say let's cast it with Aiden itself so we've got first object let's copy this and let's paste this in this second object here we'll say replace this so I show up using get method now we are using query and we say this is q2 and session 2 it should work now let's say if I want this query you can see ok record so we got an error it says is closed or because we have not replaced this with q2 okay let's run this once again you know bugs are everywhere so you can see we got the data not with the get method but with the help of queries and record this two queries here we got the first Sonic query and then they got the second select query the same data you are getting for two times now why exactly we are doing that or why we exactly getting that is because whenever you use second level cache it is by the power forget matter it is not applicable for the configuration okay so we have specified the second level cache a right but we have to also mention one more property here because by default when you second level cache a it will it is applicable for gate method not for the query for that you have to specify one more property here and the property name is its hibernate dodd cache dot so you can see if i say control space there is one more property here which is use query cache a by default this is false you have to make it true so that it will be available for query cache as well and once we have done that let's see if it is working or not if I have this query and okay no it is still going for two times the reason is when you specify that it is your second of our cache it should be available for query cache a you specify this in hibernate file right but you have to also do that for the query objects you have to say q1 dot there is a method called as set cashable you have to say true because you only don't know that we are using cash I hear the same thing need to be done for q2 will say Q 2 dot set cashable and true now while adding two times is because the q1 will mention that I want to use so this result so this twenty results should be stored in the cache a and second time it is fetching from the cache so we have tried it at both the times and if we run this code and you can see we got only one query so it is not executing the query two times it is doing only one time right so that's how you can use query with word hibernate caching hql which is which stands for hibernate query language now why we need this concept when you have when you can use the methods like get when you can use method like save update so if you can use all these methods to change the data in your database why you need another language okay just think about it now when we use our method called as save what we do we save the object to the database when we use update we normally use we change the data again with the help of objects but when we use something called us get what we specify is the class name and the primary key right so let's say if you want our object which is the fifth row of your table we will be using a get method and again we have to specify the primary key right but what if you don't want a simple row you want some you want to work with multiple tables maybe you want to fetch not the entire dickwads but some specific records I don't want to have the entire record I don't want to get the entire row I want to get only one row how do that for this you need @ql now you might be thinking about okay we have to learn another language now or not exactly if you know SQL SQL is very very easy how provided you know SQL okay if you don't know SQL then there is something called as learning curve but if you know SQL if you know what is the next are from table if you know what is select row number from student if you know where clause you are good to go so in hql what we change so first of all in SQL what we mentioned is so let's say if I if I use this query the about query here which is select roll number from student now this roll number is a polymer and student is a table name in SQL right in HQ l what if dimension is you have to mention select then you have to mention roll number but this roller is not a column name it's a variable name or the field in your class or the property in your class and then you have to mention the class name not a table name but the class name and that's why you can say this is student in small s this is student with capital S right so you have to mention the class name not the table name that's that's something remember this is SQL and this is SQL almost same right so that is exciting you know you have to you can go you can learn a new language what is SQL if you know SQL you can learn SQL if you know SQL right now now what what what's new in that I mean can we do some extra stuff and yes so let's say if you want to fetch the entire table what you do in SQL is you say select star from table name in case of hql you can simply mention from table you don't have I mean from class name we do not to mention the Select query because it is something which is obvious right when you say is the next star you want the entire table then why not go with this instead of saying select you can simply mention from class name it will fetch the entire table for you right okay can we do join see yes of course instead of using two instead of using two table name you have to use two class names can be used group I of course can be used order by of course everything is possible again how to do those things that we'll see in the in the practical session but it is possible to use all those things which we do in SQL in SQL as well okay now there's one last thing which I have to point you here so if you talk about SQL let's say if you are working with Java and database and you are using JDBC now if you remember we used to use these all set so when you father query you get the data in result set and then what you do is you apply those beautiful while loops and then you try to phrase the data in case of hql you catch so when you file a query you get the list of objects which means you can fetch all the information with the help of enhance for load it's that simple okay so SQL is simpler than SQL okay can be used okay can be use SQL in hibernate and yes we can also do that so there is something called as native query so if you want to use SQL NH in hibernate instead of using SQL we can do that okay with the help of native queries and this video we'll talk about how to use hql in hibernate now if you're wondering about can we use SQL yes we can so we can use SQL and we can use SQL in hibernate in fact we have seen that in theory right we have talked about why do we need hql so let's see how to implement that in this in this project so if you can see we have this project here right we have this app dot Java class then we have student class and then we have this hibernate configuration so I made some changes here again I have changed my database flow MySQL to pause grass and then I'm also changed the student class see instead of having the laptop object we have removed that we have say we are simply saying roll number name and marks right and let me just go to I mean I'm just using some simple class here no alien all the stuff let's go with simple thing roll number name and marks so what I want to do now is I want to save this project into database I want to save the data in a database so for that first of all to use hql what I will do is I will insert some values into database okay I will be using some random class to insert the values so first of all I will use a for loop here we say insert I I the value of I would be 1 and then let's go to let's say 50 values and then we'll say I plus plus and that's weird I plus plus and then here allocate object of student I will say student s equal to new student ok and now whatever I do is I will set the values I will say s dot first thing I will set is set roll number we'll set the roll number as I itself so as your value increases you have you can it is the value and then you can say this is s dot set name I will say name and plus I so that it will print name 1 M 2 and M 3 and then we'll say s dot set marks now I want the mark should be random but for that I will use a random class that's a concept of kochava gain so we simply say random are equal to new random okay and then we'll say what we'll import the package for that control shift Oh your package is imported and here we'll say enough smarts we'll say r dot next int and we'll specify the value of the max value it should be hundred because max should not be more than hundred right that's that's great and then we simply say session dot save and we save that object so it should work right so if I done this quote there should be 50 entries in the table let's run this and you can see there is no error and we got 50 entries there can you see that insert statement 50 times let's verify let's go to our post grace and let's refresh this thing and you can see we got our record all this thing it's because I have added this stuff here I don't want that stuff okay let's you want this once again okay now let's refresh okay doesn't matter I just there the main focus here is on student right if I if I say view data and all the rules that's how you are posed this works and you can see we got this table here with 50 entries whatever you do now is I want to fetch these values so I want to fetch let's say I want to fetch the massive person with rule number seven okay I want I want this one okay how can you do that before that what we can do is we can print all the values so let's go with all the values so for that let me remove this stuff because the the thing is done and I will change this from auto I mean this this will make it update so that I don't want a new table every time right so now let's fire the query now how do you file a query it's very simple simply create an object of query so that's a query interface which you have in persistence okay not in position but we'll use hibernate so let's say control space and we'll say hibernate and we'll say Q equals u now how do you get a query we'll set session dot create query so that's how you create a query inside this you have to specify the query now again we are not using SQL here we are using hql so when you say you want all the we can simply say from and you have to mention the entity name which is student so we say student and that's it you got this query but hold on how do you fetch a value now because this just this is just a query right so to fetch the value we can simply say Q dot list so does the method we have to use to face the value so we say Q artless and we'll save this value because Q dot list will give you least value you can see it gives you a list so let's go back to app and let's say give me a list there so we say list and we'll specify whatever student object we want I want I want these top students and I will say this is students that's we're okay students so you can see we got at least here of course we want to put the package there let's import the package which is Java dot util okay package import done and you got the least now it's see it's so simple we are simply saying for all student it is not giving you the result set which used to get in and JDBC right and you know trust me everyone no one likes result set okay it is just the way because it was there so we were not having any issue but now instead of fetching the results that we are getting a list of values it is red easy to work with list right so we can use an house for Nokia and we can say student s : students now once you've got list we can use enhanced for notifies the value let's print all the values let's say print s and that's that's it if I run this code okay you can see record all the data here you got roll number you got name and you also getting marks right so let's go to student and yes we will be caught all those things right so that's how you can use from student to far the query but well now let's I don't want all students I want specific student let's say I want student who scored more than 50 they can use where cross as well again this student is not a table name okay this is not table and this is the antique name you can specify the where clause and you can say where those number or not roll number but marks is greater than 50 it's that simple if you know SQL this is very easy and if I have this QWERTY you can see will be getting on it those records those people who are getting marks more than 50 you can see all the records here so regard people who got masks let more than 50 right let's say if you if you are getting a specific value let's say I'm not getting a list I will say marks equal shoe or the roll number roll number equal to seven I want I know that it will give you only one result in that case instead of saying list what you can do is we know that it will return out only student object not the least I will say student student and here instead of saying list you can say Q dot there's a method called as unique result because unique result will give you only the unique result I was giving you Adam it says you have to typecast it because it will give you the object you have to typecast it with where student and done and instead of instead of using for loop we can simply print the value of student right and that's it we can also use back Clause here can you see that record the data with is with with rule number seven with name seven and forty-nine okay again you can verify that in your table as well right so that's how you can you can use hql query here can be used some more queries yes we can we can say we can create tables we can use join tables we can use what else we can use your okay we can also specify the columns okay if you don't want the entire table you know you can specify the columns you can say select star from student you can also specify select R consonant run this query and okay now in this case whatever do is o star doesn't work let's use something else let's specify all the values or all the columns we say roll number name and marks now this will not work the problem is okay so what what is that just happening here the problem is what is not working is when you specify the columns it is not sure that you are getting the actual object okay so what it says is instead of using the instead of getting a student object what you will get is you will get Li stop stirring it we will get an array of strengths okay again how to solve this that was see in the next video but in this video just remember we can use SQL queries in the next video we'll talk about how to solve this issue okay what else we can do here and thus yeah we'll talk about that in the next video and in this video let's continue with the last thing which we have done which is SQL so the last video when we find a query which is from student we got the entire table right and then we save that into a list and everything was working then what we did is we applied a where clause where you are getting a unique result it was working but the moment you specify the columns you know we have specified roll number name and marks that's where the problem started right because when you use select query that means or whenever you specify the Select part it means that you're not fetching the entire row if you're fetching our specific columns it will not return new student object now so what it will return is it will return you the object now while object why not string integer it's because roll number is integer name is string and master integer so if you want to have any type of data we always go for object right and we're not getting one object we are getting three objects they roll number name and Mouse right so for that you have to create an array so we say object array on the right hand side as well we have to say object array right we have to type cars with the object addy and now since we got an array we cannot use normal student print right we have to use a loop here we'll say loop which is of type object I can use enhanced for loop here we can say object Oh equal to student okay and let's print all the values let's say I want to print oh and if you have this code you can see we got seven name seven and and 49 right so that's all I record here we have one more ways of instead of using a loop what we can do is we can say system dot out dot println we can say oh of zero then we can use a colon here just to submit those things we can say oh of one oh not oh it's a student right so we'll say student of zero student of 1 and then plus student of two that's that's about three records right so now it will you will get one go which is seven name seven and forty nine so you can see that there was a there was not of space between this we wanted a space here right so will you : okay that's perfect now let's run this and you can see recording data as we want it but let's say so that's how you can fetch the if you use a select thing but it's if I don't use a we're close now let me remove a where clause from here now what will happen is the moment if I remove the where clause they could not give you one leg or it will give you the exact table right now you cannot show that in one record you need to use a list of students so we'll say list here because again we are going for the entire table let's import the package for that finished Java the QTL and let's type cast it with list of objects right so that object added is for one row and least is for the list of rows right okay now there's some change here we will name it as students because we are not getting one student and now we can use the ion house for loop here we can say object array : students object array student : students now what will happen is from this student students list it will fetch one one student and you can print all the values and that's what we are doing here right and that's true on this code you can see we caught and error oh now we cannot use the eunuch results right because we are getting the entire list so we have to use a list that's perfect let's go on this and you got the entire table so that's how we can use a select query with with by using the object array now can we do something more here oh let's let's try let's say I want to fetch okay I want to fetch only those values which I'll get then but greater than whose know whose mass is greater than 60 so I think again we can do that we can say we're know lumber is greater than 60 now sometime what happened or not low number but Marx Marx is more than 60 now what happens is let's say if you have two tables and both this table have the same row or same column example let's say we have student and we have our certification person any-anything and did both the table has the same column it there will be a conclusion right because we can use joins here we can have two tables so we have to be very specific which table marks I'm talking about so that means we have to use allies with students so I can say student s and we can say s dot marks even we can do this and if you're on this code you can see regard to the output which all the all these students are having more than 60 marks right so that's how you can apply the where clause can we do something more here yes we can let's say I want to fetch only one result I want to find this sum of marks we can do that we can say some of marks now in this scenario we are not fetching one record or we are not fetching the we are not fetching the columns right we are just watching one list so I will remove this I will say it is just a list because we are not getting an array of objects will simply see a list and now we cannot say object I am simply say object and we'll just print student marks now okay in fact this column name is wrong we'll say marks and we'll print box will it work and you can see we got the addition but hold on why do we need at least because we need to be we know that we will getting only one thing right so we can simply say unique result and we can show that in the type object right and we can remove this with lace from object and then we can say this is total marks and you can train the total marks right so when you know that you are getting only one data you don't have to use at least here we can change I guess we can also do one more thing instead of saying object can be say in teacher because we know it will be getting we will be getting in teacher right let's try okay it works and you can see we got the output as a record an error it says it will return your long but that's the problem it will some returns you law okay so you cannot typecast with integer you have to say long on this side as well you have to use long and now let's run this and it works right so that's how we can use all those things the important point to remember is whatever you can whatever you can do in SQL the same thing is possible in HQ well okay we have one efficient way okay so can we do one more thing let's let's try what we'll do now is let's say I don't know this value so this value will be coming afterwards so let's say I have a variable here which is ain't Marx and let's say the Marx is 60 I cannot say Marx but let's say B so int B is 60 and in here I want to specify that variable how do you specify a variable if it's very simple we can say plus B right if we don't know the value you can add it at the end and let's run this it works but hold on I'm not a big fan of concatenation right because concatenation always gives you trouble so if you remember in JDBC we have done with a bad statement can be used purpose treatment here okay we can try so to use prepare statement here we can simply say : B and then our job is done we don't have to we don't have to use that the pass statement stuff we can simply specify : it will work but hold on before before running the query we have to change the value so we'll say Q dot set parameter for the first position I will say the value as B or not the first position but for B let's see if this works and okay is it zero which starts with zero I guess okay there's one more issue parameters are based at one okay that's weird so what you can do is you can simply use B here and now it will also be will be replaced by B okay so this B which is : B will be replaced by this B so anyway you want to specify the value later you can actually use : which will act as a positional parameters which we in Jenny BC we have done with preparing provide the same stuff easier so this is how cool hql is okay so in the in the next video we'll talk about some more stuff over hql in the last video we have talked about hql right so now you know how to use hql a different language which is almost same like SQL but we all love SQL is it exactly but let's say let's say if you want to use if you want to use SQL in hyper that is it possible let's try let's try so what do I do now is I will go here and if you're filing a SQL query let's go for SQL query so what I want here is let's say I have a student table right we have done with that so in touched when table we have roll number we have name and we have marks I want to print all the students whose marks is more than 60 the same query which we are done in SQL let's see how to do it so when you work with hql will you be create a query object right but since we are using SQL in hibernate we have to create object of SQL query so looking for SQL query and we'll say let's like let's say a query name itself is query and then we have to say session dot now in case of SQL we have I mean hql we have used could create query right here we have to use create SQL query in which you have to mention the query so I will mention a query will say select again our favorite query with a select star from student and we specify a where clause we'll say where marks is more than 60 and we are done it's so simple we can simply specify select star from student where marks is greater than 60 but how do you execute this to execute this query we have to again be happy use the same thing which we have done in hql right which will say earliest again the output will be leashed right so it's the least and we'll say students equal to let's import the package for list that's done and then we'll say query dot so there is a method called as list right but I have just one query here if I say query dot least and if you are getting the list how can you fetch the values because this is the least right if you if you try to print C this will work I mean this you will get the output but that output is there inside student and that's us that's at least format how can you print this elements okay so let's try the problem is if you try to print this values because the type of the least is object right so if I say object o : students students and if I try to print the value we'll say print oh and if we run this query you can see we are getting all object reference type and all the hash ports we don't want that what we want is okay what we want we want exact object right for that what we'll to do is we have to specify that we are using a list of students right so when you say least often and how it will work okay let me just think about it so what we can do is we can say query dot so we can specify add entity because what you are getting is not normal thing right we are getting an object of student will say student dot class right so we get shouldn't dot class now we will be getting this students object and we can typecast it here so we can say this is not a normal thing this is a student and we can change here itself we can say what we are getting is not a normal list but it's a list of students I hope this will work now let's run this and here we go can you see that we got the list of student okay so that's how you can fetch the you can you can work with SQL in hibernate so we can specify a select query which is your which which is your normal SQL query we can specify identity by specifying what type of values you are getting and you're getting a student object so that you can use typecast you can use generics here with a student and we can print all the values this is one way now let's say you don't want to print entire table you want to specify specific columns so instead of using star we can specify the columns here so let's say I just want roll number and I want marks I don't want names okay odd just to change it let's say I want name but I don't want roll number in this scenario since we are not fetching the entire student we cannot type crosstab instrument right let's remove that okay but I will work now so I cannot even say identity student because this is this but by specifying this you are saying I will be reading the entire law we are not getting that for that if you want to specify okay before that I have one more thing so this will be not student this will be object and here I can say okay how can you print it now so what you have to do is when you say you're fetching name and marks you have to specify that we are getting two things that means so when you execute this name will have a value and the marks will have a value right so name and the value so it will be a key value pair so it will be named as a label as a key and then the name actual name will be the value right and marks is will be the key and the actual marks will be the value for that but what you do is you have to say query dot so there's a method called as set result transformer and this you have to specify criteria dot allies to editing map what it will do is it will convert your output into map format okay so that you can use it okay how do you do that so we say from student every object will get a map first you have to type crossed it with map right so once we got an object here we have to typecast it okay we'll say map M equal to we'll say oh but hold on always a normal object right say typecast here itself you have to say map let's import the package for map okay now once you got the map we can actually face the values will say Oh dot get and you can specify the key here the key is name then we'll use a colon and then we say Oh dot get okay not oh but M I made a mistake here so it should be M m dot get and you can specify marks quite simple right so you got map and you are getting the output will it work let's try let's try and you find on this query here it works right so that's how you can use SQL in hql in hibernate SQL in hibernate so that is your yeah one more thing this SQL actually called as native queries so this is called as native queries okay so that's it we have talked about SQL how to use SQL in the hibernate and in this video we'll talk about hibernate object states or the position lifecycle now when you work with hibernate we use session now in session we use different methods right we use we you say we use update or we use delete now when you use this methods there is something which is changing behind the scene which is your states of the object now it is applicable for JPA and hibernate as well so what happens is we have different states so let's imagine we have a start state or we can say start state but then we have two things in Java for sure right doesn't matter is it hibernate is included or not when you create an object that's a new key that's a new object for you right so we got a new object and then once you've done with your object work you know you normally notified or you make it any cheaper for garbage collection maybe by completely scope or by writing liquid or null so we have these two states for sure but for every project or for any every object we have new and we have the end state or you can say destroy state but in between this if you can put hibernate so however it has its own state right the first one is let's say you've got the object and my favorite class is alien so I will say I have an object of alien here now this alien will have certain features let's say in an ID alien name and alien tech or the favorite technology for that particular alien example if I get at my object that will be Java so that's how I love German right so let's I have said I have set all these values the moment I do that the moment I create the object in hibernate that becomes a transient state so your object in a crisis is in a transient State now what exactly it means so transient state means if you do any modification with the object and if you set any values and the moment you close the application or the moment to destroy the object that data I mean you'll you will lose that data you cannot get it back so if you want a data back again you have to make sure that you should persist it right and that's why we have the second state so let's say if you want to save this data so in hybrid if we do that we say save or we say persist so the moment you say same all persist your object get into persistent state that the second state you have the first state is transient by default all the objects are transient the moment you try to save it that becomes persistent now in possessed whatever you do with the object it is it is dead in the database in fact this Dyck linkage between your object the Java object and the database database row so every time you modify if you see if you modify anything in the object that will be applicable in the database as well yeah example let's say if you call the object okay you got the alien object and you say okay I want to update some I want to so I want to assign the data and then you say save but what happens when you try to change the value of the alien let's say if you want to change the technology for an alien let's say I love Java now but maybe in future I would love something else let's say a blockchain maybe let's say Python so if I change that value after saving still it will be applicable it will it will get updated it adapt database as well because we have not we have not removed the object from the persistent state so what do we do with the object will be applicable in database as well yeah so we have two states first we have transient and we have persistent the next one is actually detached so let's say you are working with the object and the object is the thing is there in the position of the persistent state and you want to modify something every time you modify it will affect a debase and you don't want to do that now maybe you want to pop you want to perform some operation and that operation will should not be that in the indicated database so before doing that you have to make sure either you commit your session or you close your session or you have to detach the object from the session or from the position context so we can use method like detach to do that and they're going to do that it will so your object goes from persistent state to the detached state okay we have talked about three states now we have transient we have persistent and we have detached in some times you can imagine detached and transient as the same state because in both the state it is not weighted to database right it is no there is no persistent between your object and database but yeah we have this three state we have transient we have we have persistent and we have detached but let's say you have removed the data from the database so you got the object and that is in position take and now you want to remove the data maybe if I delete all by remove method the moment you said in it it goes into a removed state so basically so it is really the Java so that object is there in Java but it is not any database because you have removed it or because you have deleted it so you have three you have four states now you've got transient do you have persistent you have detached and you have removed yeah so when you get the object let's say new state from new it goes so transient from transient when you say save it goes to persistent from persistent it goes to detach when you detach it so it's just like you have a database or you have a position context you have the object you have detaching it that's detach state and then when you delete the data or when you when you remove the data that becomes your remove state but the question arise what if you don't want to clean your object or you don't want to save the returned the database when you want to fetch it now what happens when you fetch it let's say if you if you have a new state and you want to fetch data in that scenario you don't have to make it transient first what you can simply do is you can say fine or you have different methods like you have get or you have find so when you say get your object by default getting to persistent state because you are fetching from database right so it is a it exists in the persistent state which means after doing get and if you if you modify the object it will affect database as well yeah again after this video I do have the implementation of this so you will see that our implementation later but then you are so they have wrong from the new so when you say find or get it will it will get into persistent state right so there's a shortcut as well but let's say after in transient itself if you don't want to continue with the object you don't want to modify or something you can directly destroyed so all the object will be eligible for garbage collection after removing it or detaching it or after or this transient state itself yeah that makes sense right so yeah that's it how that's how you make it work in fact you can go back from detach to persistent state with the help of save method or update method yeah so those are the things available in hibernate yeah so those are the object States available again how to implement this that we'll see in the practical implementation in the last video we have seen the theory of hibernate object States or which is also called as persistent lifecycle in this video we'll try to implement that now before going ahead let me show you a class here which is laptop so basically are walking with this laptop class which is actually an entity which we are using here and it has three properties we have an ID brand and price and we already have a table with this name in the database and again that is done with the help of hibernate itself so if I open this mask you'll and if I say this is my tea table is basically which is alpha and then in this we have this laptop so if I select star from laptop you can see we are we have all this brand and then price and L ID the last one is once you know one one to Delhi India and 0 let's focus on 51 so let's say 50 after 50 we have 51 because this huge gap between 50 and 1 1 1 so what I will do is I will add a new object which is I will get a new object which is which will have 51 now let's go back to app and let's get the object of laptop so is a laptop L equal to new laptop and this this laptop will have certain values I will say L dot set L ID which is let's say 51 as discussed and then will say L dot set brand I will say the brand name is Sony I don't know anyone by sony laptop nowadays but then we have a branch sony and then let set applies as maybe maybe 700 others so that's the cost you can you are ready to pay for sony laptops so we have all this we have all this data now what state it is by default in java when you say you are creating an object that's a new state but the moment you assign the values and if you take and take this into a hibernate consideration it is now in transient State now why transient is because we got this object but then this object is not getting stored in a data database so the moment either on this application the data will be there in the application and the moment I stop the application so when I stop it will be I will lose this data I will lose 50 when I will lose Sony I will lose 700 I will not be having this data so in order to have this data somewhere I have to persist it or you can say I have to save it and that's why we say we have V V so that object goes into passes and state so I will say session dot save and if I pass the object L now this goes into persistent state yeah so if this goes into persistence it and this data will be stored inside database but only when you say commit otherwise it will not getting stored so let's say I have sent e value which is said grant is Sony but after after saving it if I change the value if I say hey I want to change this I want to change the price not the brand I want to change the price from 700 to 650 dollars I want to just give some discount and then I want to again you said use the value so I will make it 650 now what do you think if I done this code what will getting stored what will be what will be stored in storing in the database will it be 700 or 651 on this code now logically if you think in normal world or when you say save it should get stored right and that should be the value which is 700 and after saving we are changing the price which is 650 so we are not updating again right they are not trying to save that data again so it should be 700 but the moment that on this code and if I go back to my hospital bag when she prices the next start on laptop if i refresh if I go up you see that we got Sony and we got 650 that's weird we were expecting 700 here but why 650 is because the moment you say save your object is still in is in the persistent state so what they would make whatever changes you make with the with the I mean with the object it is reflected in the database as well and if you look at the console here initially it is it is saving it is saying that you are storing the data so insert query but the moment you updated the data it becomes it fast the update query yeah now so this is your this is what you say a transient so from translate your going into persistent state but let's say I want to make one more change first of all I will I will get a new object now just since I'm making some changes I will say let's go for a new new brand I will say Lenovo and then deploys for the Nova is let's say $800 and after some time I'm making it changes to 750 dollars and then so I want this to be persisted so of course I want to change the database value as well 750 but due to certain operation let's say I have done some statements here and these are some operations and after all these operations I want to make some changes because I just want to change the business model I don't want to change the data in the database so I always say customer comes to me and say I will inform customer ok I'm giving you a special discount but then that will not be getting stored in the database so for database for those people moving 700 750 but for you I want to make it 600 but the moment I say L dot set prize what do you think what will be the final price so it will be 600 of course because it is still in the persistent state what I want is I want the changes till 750 I don't want to make the changes in the database so in that point what you have to do is you have to say session dot there's a method of detach the moment you say detach this object L now your object L which is a laptop object is not in a persistent state it isn't attached state so whatever changes you make here it doesn't matter to database and let me show you the output cipher on this code you can see it is filing the update for us that she's not showing there it's not fine in the update coding that's weird what's wrong okay let me just go back there and let's see what happens oh I'm detaching it before they come out that's weird okay so it's still but let's see what happens so if I say thing okay so you can see it is 650 oh it's not even took it is not even getting stored because I'm detaching it before commit that's my bad yeah but then so do so let's just do that after commit I will say cut and I should be doing this after commit and if I run this code you can see this updating the query only once and now if I if I go back here and you can see we go at 750 we have not we have not got 600 so that's the power of detach in fact after commit we don't even need to touch but then let's say if you are using or transactional things or if you're using some other type of maybe some other framework to implement this maybe you have to use it detach at that point but anyway you've got the point right so after commit all of the detach it will come in to detach state that means what do I make changes you make with the object that doesn't matter so that's how you implement these things in fact we have one more which is removed so you can when you say a session okay when you say session dot remove and if you pass the entity L from the 20 database it will remove data that will take this object into a removed state so that's that's how it works so that's how you can use all the states we have transient we have persistent we have detached and we have removed in this video we'll talk about the difference between get and load method in hibernate know if you want to fetch some data from database what we normally do is we let's say we have this class called laptop and I want to fetch the data from from database again I do have a table which in which we have so many rows we have so effect if I can show you you can see we have data which is an ID one and two and one and then price so let's say if I power supply and it says six and based on say and based on the idea I will get the good D laptop object all right so let's do that here so what I will do is I will go to a app and I will say laptop and I would say lab not basically there are multiple ways of fetching data you can use hql you can use criteria or you can simply say session dot so there are two methods we can using but you can't do that in fact we also have find but then that comes from JP a perspective but then we have get using get if you pass the laptop I mean if you pass the class name so that you will get the you don't have to do the casting manually and I will pay I will mention the ideas that says six and then I will get the object so if I try to print laptop you can see it works you can see if we run the code or you will get the object here and that that works right but let's see if I don't use get if I use load so another method using which you can using using which you can fetch right and the moment I run this code and let's see if you're getting some other output no we are getting the same output so two methods do the same thing in both the cases we are getting the same output but what's the main difference now the main difference comes in the perspective of performance or the type of exceptions throws now what happens is let's say if I use gate ok if I use gate and if I if I don't use this object so I'm not using this laptop object I'm just fetching it ok I'm not I'm not printing it that means or maybe not just printing but then I'm not using that object so the moment I run this code and let's see if it is if he's finally query you can see it is firing the query so the query is fired and it is hitting database so every time you use gate it will hit database but what about loaded now what load does is load will actually not hit the database let me show you what happens when you say load now when you say load it will not fire the query can see that it is not filing a query but yes when you try to use the object then it will then it will fire the query so if I try to use it and if i underscore if I run this code you can satisfy in the query in fact if I if I apply some thread here which is which will ask it to wait for some time if I say 24 let's say 3 seconds so that we can see the difference there and if I say surround with try catch try catch if some applying a slip here for 3 seconds basically and if I run this code you can see it is not final query and I'm going to say use it and after 3 seconds it is fine in the query that means load will not give you the object at the same time so what it gives you is a proxy object so get will give you the object but load will give you the proxy object now this proxy object will be a blank object it will it will simply specify hey we got the object but it's not an object okay it is just faking it's not there's no there's no actual object it's a proxy object now what happens so this is this is the difference between get and load so get will give you the object load will not give you the object load will give you object one day only when you want to use it so it will basically give you a proxy object when you say to Zoda yeah now this is helpful when you have when you when you opt in you depend upon some other object let's say to create an object of let's say alien you want object of laptop so every agent will have a laptop in that scenario if you want to make an object of alien you can you can pass a proxy object of laptop yeah so we don't normally use load we always use get for anyone want to fetch data but there are certain scenarios where load make more sense right something where you want to pass up the oxy object you don't want to pass the opto object you just want to put on the code and that's it ok so that's where you can use load now there's one more difference between get unload in terms of exceptions or in terms of data which which is return example let's say if I pass an ID to 201 I know it I don't have any idea which is 201 and if I use get method ok so if I in my database I don't have any row with which has 201 as row number so if I run this code and you can see it pretty prints null of course I don't have a data there so it will print null and yes if we try to fetch a specific data it will it will null point exception yeah so if I'm this query you can see it throws a null point exception because I'm trying to access the data from null object or null value but if I used load what will happen now load will not give you a null let's see what it gives so the point here on this code it tows an exception and that is object not found exception yeah so gate will give you null and load will give you the exception it rows this object not one exception so you can actually handle it and you can make it work so it's some way you are getting an exception so you can use it you can use try catch you can write in the catch block what to do when you get the exception you can handle those things so that's how this load and get works and this is the difference so normally we use get but there are certain certain specific resource per tipic times the example if you want to create if you want to use an object as a fake object or just use a proxy object we can use load there so that's how you can use get and load 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 so to 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 the F of 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 if you even don't know it open it that's fine we can still try to understand what is happening here so Java database and JDBC the 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 ball or technological world we are walking with information right we are working with data so whatever we use it's all about data right if using Facebook if you are using what's that because using any interface 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 south as we process data now toast data are by default temporary data because your application is working with some data and the moment you close the application you will lose all the data what you want is even if you'll a patient said shut down you still want to persist that data somewhere where do we posit our data so normally we can persist data in a file all we can pass this data in a database which is your DBMS or a DBMS you can say maybe MySQL Oracle the host there's so many RDBMS 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 PC but again there's not so many issues which are EBC you know you have to fetch the data or so normally in Java while we work with objects and then in the database world you have those and columns how can you save the object in data in the rows and column format and that's why we have this concept of ORM so what OLS order basically means which transform object relational where you have a plus so you have a plush 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 in dotnet in half or Java so that if you languages or 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 i practice of the tool we have top things so there are so many tools I've been able so you can use any of this tool then you can make it work so using hibernate using I back yes 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 and so you have a alien object inside alien object you have a de ID in your name and in in technology or maybe you can also talk about this salary of a Dean and which laptop alien use or which technologies working for give it and discuss about this but then let's say you have in an object and you want to store that in an object editor base so in geni we see what you do is you fetch one one glad when you store it that's no good that's little bit of a right so what others say is take the object and save it into DW directly that will be awesome so to implement or whether 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 like today we are using hibernate and and their application is built using hibernate now let's say after two 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 in there if they are using the same type of structure of same specification unfortunately all these tools they were having their own way of working there in their word 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 persistence API it is basically a specific age so all these tools maybe hibernate I bet they stopped 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 is the advantage of using JPA in your project but yes if you are building an 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 way down when we have JPA then vibrating 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 I buy tests you cannot simply make a JP application without any of this tool yeah so - it is important even if you're using TPA 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 boot applications and then if you want to work with database we normally go for spring boot 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 spring there and it we get a new project here and I will say this is a mammal project ok base mammal just 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 so next ok so it does take some time - done it from the internet yeah so it is asking for the group ID I will say globe ideas from dr. disco and the project name I will mention is JPA demo I can you can have any project name doesn't matter click on finish and you can see the quite 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 a main method yeah so I went to class which have methyl so that I can do all the application so even before continue with anything you just want to verify if this one works on this it works works that's it that should be a first step some certain has a job application and it worked you can see we got a low boil but then I want to implement a JPA type of thing now of course to save data somewhere because JPA will be helpful for saving eaten right so we need a database and fortunately I do have mask you are installed so you can see in my machine I'm using mass scale you can use any DBMS UT you want it'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 mask you are specifically I'm using Maskull workbench to handle my ask you again you can work on command line as well if you want it but on big I'm a good big fan of GUI so I have a workbench here and the Deaton is name which I paid it is Naveen entire database I have a table bus alien so I 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 torch other big data three debit dotnet and forth and a PL ml ml and then turn off well I thought so we have this five entries there now I want to fetch this data using application oh 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 my trick here I will say new class and this class will be alien and the second finish so we 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 tech so your columns will become your variables in the java java class and then these three variables need get a status of the right click here I was a source generate get a setters and okay so we got our class and record about get as it is we also need our two string method so I will say so generate two string so that if you want to print an object you can print the values of waiting show up into the hash code 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 alien 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 lien object with ID for now or not for three now in this world of course not right B Allah we are working with Java technology this is not Java this is normal English right how can they implement that here so to work with JP here we have to do some settings right JP a is not a Superman right we need to want it to make it understand what you're talking about so how many use JP here so they are further care to do some settings the first thing is JP a has this special method for us find now in this find you simply have to mention for and then you have to also make the class name so alien dot class and then you have to mention for 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 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 or if you study clock starting method you can simply use the class 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 areand class is an entity for us right so to manage those entities we have any dimension because in your application you might not have one table or one class you might have 10 classes so 10 entities so we need a manager to manage all those entities so yes we got one entry manager but question is how do we instantiate it unfortunately the side side that's an interface we cannot even say new new 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 nav and dependencies I have not mentioned that I want to work with JP or hybrid 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 why is 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 a hibernate so when you search for hibernate it will give you a hybrid depository in fact you can just go to the website of level depository and here you can search for hibernate do and that will give you the same same least oh it which is not give you the same register let's search for I want it again it is giving a different thing so you can see the first thing we just go hibernate ORM functionality again if you have seen my hibernate in task 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 are 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 to 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 many dependencies it will download all the different disabilities dependencies for you now it's also depend upon your internet speed how fast it should be mine is based flow today is taking some time now by that by the time it downloads it's the high burden dependency we want one more now see if you work with MySQL you need a master connector right before working with what I call you need whatever connectors different DBMS have different connectors to work with and for this one we need to use MySQL connected as well so by the time it gets downloaded I will search for master hunter the first one and I will download which version I always prefer this table one this looks table there is no problem with this let's copy this one and go back here paste and you can see we got hi my 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 mask 0.0 as well now once you got these two two things we can simply say control space now and you can see we got we got the inputs and I first enter record edit a manager belongs to persistence package yay so that's the power of these dependencies okay now how do we cannot determine to do entity manager now unfortunately we have to write one more step to implement that to create on setup entity manager way to gate 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 you know so we 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 affinity manager you need any divisor factory so let's say e-m-f any object will do but in question eyes how can I click on the top entity ID Factory good point okay so what we will do is these are there's a method called as oh there's a Pascal as persistence persistence dot is it a class 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 anything manager factory and yeah something here again what is that something 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 edit matter of fact when you can say create a new manager ok so just repeat what we have done we have painted two objects any dimension to dimension factory and now using e/m which is the image of factory you can say ii m dot find and you will get an object so what do you think if i had done this will this work of course it should not work because something is missing now nowhere we have specified which DB miss you want to work with I mean we have specified in the form file which is this this form file here but then how do your project knows that the app knows which debase you want to work with or which database you want to work with all what you use them in password 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 in JP a JP a with hibernate or spring framework or starts framework we do specify the contradiction 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 instead accessibility to special folder inside Java is it inside Java let's verify no inside bean I want a special followed up on his sources I will say pull it 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 INF and inside this folder you need to create a XML file you can buy any kids visit by the properties I would say 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 there XML files thanks to spring thing but we also we have Java based configuration so I will say persistence dot XML and here we have to paste that you 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 you have to specify properties our 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 settings 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 SP you want in you to p3 you can create multiple positive unit and then here you have to mention these properties so we have a driver name we have a URL username 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 P you can be anything it can be ABC it can be my J PAP you 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 will this work everything looks cool right let's verify so whatever you do is I would right click here I will say one ass job application and you can see we got error now what LLB got is it says unable to locate persister which is alien it's dead the class is available here why it's not able to find it it's because when you work with JP a JP a bicycling works with entities what is entity so we have a class and we have a table so something in between we have entity how do we convert this glass into a TB how do we mention that this class is not a society 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 to link that's it now will this work no no let'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 will it is it successful the way you can verify this by printing the object right so I feel on this if you printed 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 amel you can change the value from 4 in fact you can also if you 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 say you can fetch the value how can you save the value because I want to I also want to see how did we save it so whatever I do is I will go back here I will say alien e equal to new alien and then I will specify some values I would 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 tank ok we should 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 say this it's very simple you can simply say e F now to fetch we are using find this see the method 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 this painting 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 don't ignore that rule and I would say it run this code let's verify is it Maria then no we don't have Maria there 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 transactions to manage acetic acid properties so here we before saving we have to start the transaction and before and after saving you have to commit the transaction and the way you can do that is by saying 'i M 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 breathing getting windy now 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 9 Maria Maria and we got Hardware simple yeah so now let's see what it thinks they have done we have simply created an event project we created this app class of the order ticket they already had that class here in this we have Kady the alien object and then to call this our important one is entity manager and then it image effect way so using factory can play Tanja top entity manager we just to specify the persistent unit name which we defined in the XML file again this XML file should be there in the resources folder or don't worry I will push this project on the github so that it'll access this project or you will find the link it is discussed 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 the URL the user name and pass will depend upon your machine and sir alien 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 application 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 to change the or what if you don't have a table can you create table by yourself you understand that those terms when you start work when you start reading hibernate series so after watching this video if you enjoyed it different like button but then also watch I will 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 you can also do it with hibernate Lloyd 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 everything what you do is hibernate code example these two objects will be based on hibernate framework or hibernate packages the thing is in future if you want to change it we need to change the so let's say from hibernate if you want to move to eye biters or maybe toppling you have to change this 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 win tails 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 doesn't 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 duplicate like button and do subscribe for further videos
Info
Channel: Telusko
Views: 699,164
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: JR7-EdxDSf0
Channel Id: undefined
Length: 199min 40sec (11980 seconds)
Published: Tue Jun 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.