Spring ORM using Hibernate

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] spring project has lot of modules right we have talked about spring code which talks about dependency injection we have spring MVC which talks about how can we Implement MVC pattern with the help of front controller in Spring and then we have also work with spring rest that we'll see in in the future videos but what about the database what if you want to connect your spring application with database now even it doesn't matter as it spring we normally if you want to connect with Java normal Java application with database we have to use jdbc right now we all know the pain of jdbc because in Java world we work with objects right so we have to convert our objects into data which will be stored in a table format to solve this problem we got o RM which is object relational mapping so imagine a class here and a table here so we can map it right because the class name will be your table name we have some fields in your class you'll be having of variables so you can map those variabl with the table column right but what about the data what about the rows there now for one class we can have multiple objects in the same way one table can have multiple rows and each object here will represent one row there think about this let's say we have alien object and alien will have three variables a ID a name and let's say age so we can have all this in a table columns so if you create the first object for alien with three different values that will be one row there if you create second object with three different values that will be second row there and that's how we can do the mapping and that's where omm is very famous but how do we Implement omm and that's where we have jpa which is Java persistent API now the actual implementation for jpa maybe you can use hypernet there so that means we have to build the application we have to work with uh hynet and lot of configuration can we use all those things in Spring and the answer is yes because spring provides you dependency injection so the instance for hyb can be created in Spring framework and that can be done with the help of spring orm so basically you are using a spring application you are using hibernate which connects with myql which connects with database so we have a huge chain there so we have spring we have hibernate myql and database in between you will also have spring om which will connect this two right now the advantage would be so the instance creation will be done by spring omm for hypernet the configuration will be done in the spring configuration next what about transaction what about begin transaction commit transaction will be handled by Spring transaction uh so how exactly that works we see in the Practical [Music] video so let's try to implement the database connection here so what we trying to do is when you run this application and that we are doing it in normal spring MVC we are not doing it in Spring boot now so yes when you talk about normal spring MVC on normal spring we have to do a lot of configuration so just be with me because in this video we are going to do a lot of configuration so let's get started the first thing is when you say you want to connect with database we have two option you can use normal jdbc or we can work with hynet now since we work on big applications better to go with hibernate because it can handle your transactions in a good way you can achieve omm here so let's do that so what I will do is the first thing we need is we have to go to the pom.xml file and we need to add certain dependencies so so basically we have to add approximately four more dependencies here in fact five I guess so let's let's get started the first one we have to add is now see when you work with spring MVC and if you want to work with hypernet you need hypernet code dependency that's very important now when you say you need hypernet code where you will go so you have to go to the MAV repository so you have to go to this website which is MAV repository. comom and here you have to search for hibernate code so in search for H hibernate code you can see the first link is hibernate code you can select that and you can pick up any version I preferably go with the latest one because it has a good number of usage uh let's copy that and go back to your application and paste that's the first dependency we have added we have to add some more because when you say you want to connect spring and hibernate now hibernate being a omm framework you need to add a dependency for to connect spring and hibernate here which in this case is spring omm so or will be setting between hibernate and spring so let's open that I'll go with 5.1.4 because that's the spring version we are working on copy this and go back and paste when this is done now what next now when you say you are working on Spring and hybernate whenever you work with hibernate we have to handle transactions so you have to begin transaction you have to commit transaction but what if you want spring to handle it and that's where we can use spring transaction so we just have to add one more uh okay we have to add few more but let's go step by step so if we have to add spring transaction because this will handle the transactions for you so let's paste it we have added hibernate because we want to use hibernate here we added spring omm because we want to connect spring and hibernate we want spring to handle the transactions and that's why we are saying spring transaction okay now we need two more okay last two okay last two uh the first thing is uh when you say you have a database of course you need to install some database right now in this machine we have already installed MySQL again we'll see this setup how to install MySQL and how to configure it so we'll see how what is mysq and how to open mysq workbench and where from where you can download it but then in this machine I have already configured it so the thing we need here is the mql connector so the the setup we are using is mql so we have to use mql connector and for that we have to download M connector so in case if you're using some other dbms let's say po or orle uh make sure you use that particular connector so I'm downloading MySQL connector let's copy that I will go with this version which looks stable copy and go back here and paste okay when this is done now all this dependency takes time to get downloaded from the MAV repository but since in this machine I've already done that before so it is getting from the local repository now when you work with hibernate when you work with databases we need to create some connection pool maybe 10 connections at time five condition at a time and we have to configure them uh so to configure this type of pooling and to use pooling in the in the project we need to use certain external jar files or maybe a third party pulling service so to use that to to use third party service here we will be using C3 P0 again you can choose whichever service you need this is my favorite C3P0 because it works uh let's use this one I will prefer the latest one copy and go back and paste so you can see we have added all the dependencies and job is done just to give you a summary we have added these dependencies the hibernate code spring orm spring transaction myc connector and C3P0 okay once we have added all the dependencies what next now we have to configure hynet because just by saying we are using hypernet it will not automatically connect with database right we have to configure it and to do that where where do we configure this and we have done that before right so if you want to do any configuration we have this very favorite file which is Tesco hyphone salet this is the dispatch salet configuration file you can add the configuration here itself now first of all we are using spring transaction right so in the in this beans we have to also add TX you can see we have MVC we have context but nowhere we have TX so let's add that now this TX we can simply copy this line and paste it here MVC will become TX and here as well we have to say TX that's done we need to add the thing here as well so we have to we will be copying this one and let's do that here itself in fact we can add it anywhere we want but let's do it here uh so instead of MVC again we have to say TX instead of MVC we have to say TX I could have easily copied from somewhere and pasted it here but I wanted to show you how can you do it by yourself without copying so this is done next we have to add certain configuration now when you say certain there a lot of configurations so what I will do is instead of typing typing everything now it's time to copy paste so you can see I have copy pasted a lot of stuff don't worry I will explain each and every line here now the first thing you need is we are saying let's go bottom up so the first thing we need is we want to handle transactions right so we want someone to take care of transaction in this case we are saying hey Spring you will take care of the transaction spring will say okay I will do it but I need some configuration so you're saying okay if you want to handle transaction I will create an object of my transaction manager in fact uh this is a object of hnet transaction manager so how do we create the object of this so it's very simple you just have to create a bean uh so we are creating object of hibernate transaction manager and then we are saying the ID name is my transaction manager so this one is dependent on this Bean but if you want to create this Bean here we need to create object of session Factory now when you learn hibernate we have a concept of session Factory right so we work with mple sessions and all these sessions are created by session Factory that means we have to create object of session Factory here so let's set the property as session Factory but unfortunately we don't have the object yet so we are creating the object of session Factory here so you can see it says be session Factory now this session Factory is the object of local session Factory being the amazing thing is as a programmer we are not instantiating them I mean we are not creating object of them it will be created by spring framework thanks to dependency injection so you can see we got the object of this one but it needs certain properties the first property is data source now this data source will have all the configuration of database what is a database I mean which dbms you're working with uh what is a username what is a password how many size pool you need all those stuff will be done in this my data source that's one the next thing you have to mention is in which package you have all the entities because normally work with hnet you have to specify entities every table need one entity next you have to set some hbet properties one of them is Mas D So based on which dbms you're working with you have to set up this one so if you're using Oracle uh you have to use Oracle dialect if you're using postgis you have to use postgis dialect we are using myql so let's say myql dial and we also want to see the SQL query in the console so once you run the code I want to the query in the console and for that we have to set this particular property great but then we have still not created the object for my data source so it is still missing and you can see that's we have done here so we are saying we need my data source this is the object of combo pool data source now from where you're getting this remember we have added this C3P0 in this we have to do certain configuration the first one is we have to set up the driver name jdbc URL the way you work with normal jdbc and the username and password I know I should not be revealing password on the internet but that's fine uh this is the local machine password so that's fine that's my MySQL password in this machine okay so this is the configuration which you have to do I know it's clumsy but that's how you work on Spring right now this is not something you'll be doing every day right so this is something you'll be doing only once the moment you work on Project every day you will add some new features to the project but this thing will remain same yes you will be doing some minor changes in future but as of now this works okay but then what else we have here so will this work of course not this will not work let's go back to home controller and we need to do certain configuration so if you remember we were supposed to accept the request for get alien and then get aliens we need to add that but how will you do that that we'll see in the next [Music] video now when you say you want to work with database of course you need M for that right of course depend upon which dbms you work with so in this course we are working on Mas SK so how do you install masc it's very simple actually just search for for MySQL download this will take you to the official website of MySQL now based on which OS you working on maybe Windows Linux or Mac it will give you those options here okay so you can see we are into downloads and here we have option of Community Edition so basically if you are using it for free so make sure that you use Community version in the community version you can install two things in fact we need two things here the first thing you need is a community server and the second one you need need is the mascle workbench now mascle workbench will behave like uh what do you say the GUI for your for your application so I will say download on this one and download on this one as well so two downloads this is the community server now based on which OS you working on maybe Mac OS or Linux you can select that now which one to choose of course based on which OS you use you can select that click on download now this will download the mol server so you can see it is downloading the second setup we need is mol workbench this will be your what you say the GUI so now click on download so you can see both this will take some MBS so it will take some time okay so you can see we got both the setup the first thing you have to set up is the Mas server when you click on this one now depending upon which OS you use we will have different steps but it's quite simple you just have to say next next next nothing fancy uh so you can click on continue you can click on continue continue agree continue install the same steps and you have to enter the password here for your machine if you're using Mac okay setup done now once you have done this setup it will also prompt you for the configuration now since in my machine I already have the configuration it will it is not prompting me so in the configuration the only thing it it is important here is the password so make sure that you give a proper password now once you have done with the setup you have to install masul workbench so you can see uh if you're using Mac it will give you this step but again if you're using Windows is quite simple right so you can see I have installed Mas let me open that now so I'm opening my Mas work bench this is the first screen you will see and then you have to click on this one if this it is available otherwise you have to click on plus connection here where you have to mention in the host name as Local Host and whatever port number you have configured during the configuration by default it is 3306 and then you have to enter the password click on okay and test the connection if it is successful that's great you can go ahead click on okay I have to give a proper name I will say this is try okay so you can just click on this and you will see your mle workbench now here we have to get a database right so the query you have to type here to create a database it's very simple you have to say create database teliscope the amazing thing is I already have the database here so I will not be executing this the next thing you need is the table so I will say create table alien okay before this we have to also say use theis because that's how you specify that you want to use database so we'll say create alien this will have two Fields one is a ID of type integer and second one is a name which is of type Val 2 or Vare 20 which is the size and then we have to also insert data so we'll say insert into alien values uh you will insert let's say insert two values one 1 comma naven and the second one you can simply give a comma here you can say one2 comma and you can say this is let's say R so we got this two data here and then you can also hit this select query so you can select stuff from Alien the thing is I already have this thing in my database if you want to execute this click on this symbol I just want to say select star from Alien because I know it is there so I will first say use and execute you can see we have 101 naven and one2 row in this database now once you have your Mas already let's fetch this data in your [Music] code once we have added the dependencies which is uh all the dependencies here and then we have Al on the configuration and then uh on the other side we also have a back end ready with us which is your Mas your database in fact Mas your dbms in which we have created a table which is aliens so how do we connect it now first of all if you want to connect with a database you need a layer so in fact it's not compulsory but then that's a general thing we use whenever you want to connect database we normally create a dow layer so the way you can do that is very simple just right click on your project in fact right click on your package here and say new I want to create a new class and this will be a alien Dow now now for every particular table so let's say if you have multiple entities in your database let's say you have alien table you have accounts table you have user table what you can do is you can create a separate layer separate Dow for each of the tables there in fact you'll be having different entities as well so the moment you talk about hynet we have to put entities as well so that's your alen Dow here which is a dow layer in fact Dow stands for data access object so this is a class where you will have all the methods using which you can access the database normally I'm talking about the C operation create read update and delete okay I want this to be in a package name as da again not compulsory you can use any package name there in fact the moment you start using spring boot uh in Spring boot we don't refer them as Dow we refer them as repositories your choice you can still use da or you can name it as repository let's continue with da and click on finish and you can see we got a dow class here now what we want here now we want certain methods why the first method we need is in fact what I'm trying to fetch I'm trying to fetch all the aliens you can see here we are trying to we in the home controller we have this method which is get alien in case if it is not there in your code make sure that you copy this from the code which we have done in the earlier sessions you know the spring boot videos so you can copy that the only thing is in you in the attribute we have not assigned anything because we don't have the values so what I want is I want to fetch values I want to fetch the aliens with the help of get alien method so here as well inside the Dow let me create a method as get aliens so I will say public which will return me the least of alien and then this will be method name as get aliens of course you can have any method name but uh this makes much more sense let me UT the package for both so list and alien done and here we have to return the list of aliens now first of all where from where you will get it now since we are working with hypernet in hynet we have this amazing feature called as session Factory because to fetch data we have to use sessions and to use sessions we have to use session Factory so let's use it here so I will say private session Factory and here will be session Factory okay that's done now once you got session Factory here we have to import the package for that package imported now once you got session Factory here let's use session Factory to fetch the data so before using session Factory we have to get session object equal to now how do you create a session now first of all you don't have to create this session you just have to use the existing session created for you so in the container we have the session available just use it and the way you can do that is by saying session Factory the object of session Factory dot this is a method which is get current session so it will not create the session for you it will use the existing session okay once you got the session object I can simply fetch the values but how do you face the values in fact this session object here which is if I search if I type session here and the moment I say dot you can see it has so many methods one of the method we have is get now you if you want to fetch one particular element you can use get but what if you want to fetch all the elements and that's where we will be creating a query so I will say create query in this create query you have to type the query I will say from Alien this is jpql or you can say hql and you can specify the alien docl class the type of data you're trying to fetch so what this will do is this will create a query which will return you which will talk to the database which is telescope and will fetch all the aliens now if you want to convert that data into list you can simply say Do List here so it will give you the list from the table and then since it is giving you the list we have to save the list somewhere so I will say list of alien let me call it as aliens equal to so whatever data we are receiving from here will be saved in list alien and once you got it once you got your alien you can simply return it that's what we want to do right we want to return all the the aliens okay that looks fun I mean I hope it will work but there's one more thing when you say you are creating object of SE Factory we have to provide Auto wire here but you want be thinking how spring will create object of it automatically actually not if you see the configuration in the configuration we have created The Bean named as session Factory So when you say Auto wire you will get this particular object okay so nothing is happening automatically here uh things you are doing some stuff let's go back to alien da I guess everything is everything will work actually no everything will not work the thing is okay in fact first of all let's run this code and let's see what errors you get now to run this code first of all you need object of uh Alien da here because you want to fetch all the aliens so I will say alien da and then I will name this object as da I don't want to say new alien da here because I want to use spring feature and to achieve that we have to say add component here if you want spring to create object of it and here we can use our favorite autowired so that it will give you the object existing object oh we already have it here I guess I've done that before so this is what you need you need object of alien da and once you got it you can just go back here and say da dot the method name is get alien it will give you all the aliens and your job is done hopefully let me just relaunch the application and let's see what happens in fact I should run this once okay something is happening it is trying to connect to database I guess that's why there's nothing wrong here now if I try to fetch if I say get aliens uh the method the request I will say enter oh we got an error it says could not obtain transaction synchronized session for current threade what went wrong here the thing is when you say you are trying to work with session you're trying to F data the thing is whenever you work with database we need to handle transactions okay it simply means uh before anything doing any stuff you need to begin the transaction and after you have done everything you have to commit the transaction so in hynet we do that manually we say begin transaction we say commit but since we are using spring here spring omm you don't have to do that by yourself you can simply use annotation here so The annotation name is transactional so the moment you say transactional it will say hey I will take care of all the transaction beginning and commit that's great our job is done let's relaunch it let's see what other different dat we get this time and let's say enter so as expected I'm expecting an error here but let's see and we got an error it says can you see that we got a different error this time it says alien is not mapped the thing is when you work with hibernate or orm tool we have to make sure that your model classes are actually entities that's how you can connect your table with your class and the way you do that is by using add entity on top of it and here as well you have to say at ID which defines your primary key so you have to specify two things the table as entity the class as entity and the primary key as ID that's done I hope this will work this time I'm not expecting any error but you never know let's go back here and say enter and we got what uh nothing is coming yeah so we got the output can you see that we got all the aliens now in database we only have two rows and that's why we got two data here but if you have more rows it will give you that the amazing thing is in the console you can actually see the select query so we have done that right on the properties we have mentioned that we want to see the SQL if you don't want to see that simply say false it will not show the SQL in in the console let's verify the data database do we have it there so you can see that these two records are there so that's how we can fetch all the aliens but what if you want to fetch one alien what if you want to add a new alien that we'll see in the upcoming [Music] videos now once we know how to fetch all the aliens we want to fetch one particular alien can we do that in fact we also want to add a new alien to the database so if you see database now we only have two records we have one1 nav and we have one2 Rose so in this video first we'll try to add the alien and then we'll see how to fetch one particular alien the amazing thing is we already have this add alien method here we just need to say hey Spring orm just add one particular alien for me now in fact if you see the homepage as well which we have it here when you say submit will call this particular method here and then which also accepts the object okay that simply means we need to save it somewhere in fact before that I just want to do one more thing instead of calling result page I want to call show aliens page because let's stick to it let's use this one JSP file here in this JSP we are simply printing result that means if you want to call it you also have to assign to a result in fact we'll be using the same stuff uh maybe will not need it that we just need to add the model object which we have it here but that will be with name a right I want to have the name as result so what I will do here is in the bracket in double quotes I will say this is result okay that's done but then I also want to save that in database right so of course for that we have to call Dow dot save so you have to say Dow dot there should be a method named as ADD alien unfortunately we don't have any method which is ADD alien here so that means we have to go to our alien da and this is where we have to create a method named as ADD alien let's do that so let's go back here and say public are we expecting something here no so I will simply say void and I will say add alien this add alien will accept a particular alien so I will say alien now since we are accepting here we'll also have to send it and we have done that here let's go back to our code and now how do we set it so of course we have to repeat this statement so we have to fetch the s session object let's copy and paste and then with the help of this session object we can save it now the amazing thing is this session has a method named as save you just need to pass the object your job is done uh in fact before saving we have to start the transaction and we have to end the transaction and that can be handled with the help of add transactional annotation and job done right it's so simple we are simply adding one extra method with this feature and job is done let's go back to our controller to verify everything looks cool let's run this code let's see what happens so I guess server got restarted so what I will do is I will try to call the homepage refresh and that's our homepage here I will try to add one more alien which is 103 and this time the name would be let's say Kieran and if I click on submit okay so there's no error you can see we got the alien as 103 and name is Kieran now if you look at the console it says insert into alien that means we are able to fire the insert query just a final confirmation let's go back to database because we are still not sure right if you refresh this code uh you can see we got 103 Kon that means it is working so it is so simple to add the element in the record so we have done with all fetching all the values we done with adding one value or one alien how can you fetch one particular value so let's do that so I will go back to home controller and here I want to have one Moree using which you can ask for one particular alien now for that I just want to do one one more thing in the index.jsp we forgot to add the line which was there earlier so if you remember we added this in the uh spring boot project the same thing we need here paste so it will ask you for one particular alien and when you click on submit it will call get alien okay so that means in the controller we need one mode which will be get alien so I just copy this code paste it here and this one will be get alien and this is also get alien now when you say get alien you have to also accept a particular ID right so what I will do here is I will say add request Pam in a ID now since they have the same name so we don't have to do it multiple times I mean we don't have to mention that name here okay once you got Aid here in Dow it's the method name should not be get aliens it should be get alien by passing one particular ID so we are not passing all we are passing only one ID unfortunately we don't have this method in the Dow yes of course we can go there we can create the method or you can use a short here you can click on this error and say hey create a method name as get alien so you can see we got a method name as get alien this method returns particular alien so I will say one particular alien but what should be the code here so the code is very simple of course you need a session object here as well so I will say copy and paste in fact we should be saying code reuse right okay so let's create a alen object I will say alien a is equal to now this alien will be coming from database I will say session Dot the method name is get so we have two methods you can also use load here or you can use get so let me use get in this get we have to mention what type of object you're trying to fetch the alien object and the ID is whatever is coming from the user which is a ID in this case so you got an alien here and then here we have to say return a so we are returning the alien but again uh we have to transaction we have to commit transaction and for that we'll be using add transactional and I guess job is done let's verify one once let's relaunch the application and here in the output window Let me refresh the page we got two forms here the first form is of submitting the alien second one is the fetching for one particular alien I will say one2 and click on submit and you can see we got rows so it's that easy to work with database you don't have to write complex queries you don't have to do jdbc setup everything is done right and that's the power of spring with hybernate and the combination of these two is spring omm so I hope you are enjoying this there's so much you can try it out you can fetch one particular alien you can add alien you can also use load okay so that's great so that's it from this video
Info
Channel: Telusko
Views: 8,509
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, Java, Blockchain
Id: wo5Lj6OUlRc
Channel Id: undefined
Length: 31min 6sec (1866 seconds)
Published: Sun Feb 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.