This is part two session of integrating a spring MVC application with hibernate. Already we have completed the service layer, I am in the DA earlier in the video earlier in the implementation class, I have added the logger also I have a session factory. already we have discussed this if you want to create a spring MVC application and integrate it with hibernate using Spring Boot. You don't have to go in for creating the implementation class spring can create the implementations based on the methods that you have given. But to start with, here we are using hibernate as the JPA provider, our persistence provider using hibernate we are going to store the data into the database. Now, let us start working on the methods The first method is ad group.
So in theory This method you need to open a session and then call session dot save method to save the book object. So, let me open the fat I mean let me open the session session session is equal to session factory dot open session then I need to begin the transaction transaction transaction is equal to session dot begin transaction then session dot Save. Now I need to save the book object and transaction commit only when you call commit, it will be committed to the database of course we can make the spring framework called 400 transaction. Now to start with we are using hibernate or JPA provider and we are writing the code to save the object okay create the transaction I mean begin the transaction and save the object. So this is for add book next. Let me come to this Get your book method for get out of you know that you need to go in first query first as usual open the open the session and then session dot create query with a niche I need to pass the SQL query let me hit SQL hibernate query language.
So here it is going to be from book book to see entity name and books as the table name. When you are using SQL, you need to query using the entity entity name only. The return type of create query use query query, let me just add the code query query equal to session dot create query. Using query we need to call this method. The return type is a list of so you had a test booklet and I can say return booklist So this is for guitar books. Next time God gave Boothbay categories in case of good book by category, we need to pass the category also.
So the query will be something like some book B, where b dot category like Conan, the domain name, and the domain name, we need to set the value. So let me add the code first. And then I will explain. Here's the SQL from B that B that category life colon category. This is just a dummy name. For this dummy name you need to do the virginal value which is coming from the front end that is which is coming from the service layer that I'm passing it through the parameter scientists are painting it for this dummy category we are assigning the category that is coming from the service layer.
And the rest of the things are are saying just like how to call create query method. So this is what get booked by category next time. get booked by ID what is the purpose of this method, when I want to update a book in the database, what I need to do is first I have to retrieve the book. So, for this we are using get book by ID method. And again if we want to delete the book also I can just retrieve the book and delete it, but I have got a separate method to delete the book, this method get by ID is used together with update, I will explain how actually this is happening. First let me get the book for this open the session.
Let me copy this line then session dot get the first parameter is the entity class name and the second parameter is t ID. So in our case, it is look at what it is going to return it is going to return a book object. If the object is not available, it is going to throw a null I mean it will return null. So in the controller if this particular item is not available and you see have not got the objects you have to check in for null before showing it I mean before sending it to the content here it is null pointer exception. So, using second dot get method we are trying to retrieve one book. Next I have got update book in update book we are adding book ID and price what is the right approach of going in for an update method while actually there are chances that it is not necessary that the user is going to I mean it is not the user basically to say admin is going to update the price.
It is not necessary that he will be updating the price he may be updating the ESPN number, he may be updating the quantity, the author's initial the book by the book edition, all these things can be updated by the admin. But in this case if you see we are giving only two parameters, it means I am just ensuring that the admin can update only the price you Stuff that what we can do is we can pass the book object itself. So how actually to be mapped when you click Edit book or update book, a link, automatically it will go to a prop, which is going to take only the ID. Once when you enter the ID, the book details will be printed in default. Now you are given the option to edit any one off this either the name or the author or the price or the quantity.
If you don't want the admin to admit I mean if you don't want admin to edit it, you can just make that read only. So once it is I mean edited editor when you click Submit, it's asked to be updated in the database. So I am just changing this to book book. The moment I change over here automatically I will be getting errors in the service service I am PL and be able to interface so you have to go and modify the parameters in all the places. Now what is the logic for a big book Just similar to this method only, so I'll just copy it in stop there, we're just going to give it an update. So, this is that for update book, next delete book for delete book we are getting the book I mean, we are trying to replace a book by book ID and then call session dot delete method.
So, what is the first clip, open the session get the book object. Once you have bought the book object, you need to call second dot delete method. Again remember you are doing one operation in the database. So you need to call it within a transaction. So let me just copy this code and I will modify it here session dot delete Okay, so I hope you can understand this part. First you are opening the session trying to retrieve the book by book ID you are getting a book object then you are calling session.db.
It is within the transaction so that whatever operations you are calling Farming will be completed in the database that particular row will be removed. So, we have completed the dmo path we have completed the service layer but what is the next thing we need to view the details in application dot properties file. So let me open application that properties file. So, this is an application that properties right I have added details that are needed to work with internal view resolver the data source are related to the database URL username password, hibernate properties, here I have given what is the dialect we are using we are working with my SQL so it is my SQL dialect, then hibernate dot DTL auto none, what is the meaning of it. By default, it is created. When you run the application a table will be created.
When you terminate the application the same table will be dropped. Basically, this is in development process. When you're working in production already the table is created and it's sitting there so you don't have to Modify the table you are just going to give it us DDL hi Sonata was not perform the operations which are given that don't try to create a new table that is the meaning of this okay. So, this is for application dot properties file. Next, let me go back to the controller. Now, this is the controller in the controller we are we are just trying to show the homepage then we are trying to call out book prop which is going to have the form when the when the admin is entering all the details when you click submit it will come to this add book method.
From within this we are going to call book service dot add book and importing the book object. So, I hope you can understand the flow is from GST you and you are entering the values in the form and you click Add book if you come to this method. From here we are calling the service layer service called da da called session factory session. Part Three is trying to insert the values into the database what with whatever data you have given? Okay, so this is our ad group. And now once the book is added to go it has to go back to this home page.
But if you see in all the websites when you open the home page, you will be getting the list of products, the list of items, other lists of books. So, what actually you can do with you can show the list of books in the homepage. How In this method, show homepage method you can add that, but before that let us try running this application. The application has started about bookless below which I've got three links. The idea here is I want to show the list of books in the first page. What we can do is you can just add JCL using jst l core tab, you can show the book that in a tabular form, but now I'm just going to show it that understand what actually I'm going to do with from the book control I will call guitar books method it will be returned to the controller from the controller you will I will attach it to a model attribute and retrieve that in home dot JSP page.
Now we have added the logic for add book let me click Add book method add book let me update data spring MVC one book ID book ID I have given it as identity errata. I don't know I'll just check that out. But still I'm adding it over here because we have given the form field author price category click Add book giving me an error. Let me just check it out what is the error? Yeah, it is because of integrated identity value. That is it I mean hibernate tries to generate the ID for this particular table.
This table is already created by yours. So that was a conflict. What I will do was I will go This book dot java and I will come at the generated value. Now, let me save this and I will run the application first let us check whether our book is working then we will proceed further for update and delete. Now, let me click Add to cache that we try entering values now, I need to give a proper book ID then Arthur price and category click Add now the book is added let us check whether it is added properly database Let me open my SQL here select star from books this is the table name. Yeah just now we have added string m 89 tick mark it is added successfully.
Okay, now told you I want to show the list of books in the first page in the homepage. My Tomcat server is taking time to load every time. So, what we will do is we will complete the flow first and then we will come we will run the application let me go to the book controller. Now, we have got to work only in the book controller in homepage what I need to do I have to go in for calling get our books method book service that gets our books the return type person list list book book list equal to book service dot get all right now, I need to attach this to a Model Model dot add attribute what is the first name boy I mean the dummy name comma r I navitus books and the second parameter is the virginal book list. Okay, and now, let me come to the third to four methods.
When you call add book method the book is getting added So, it is going back to combat JC, when it is going back to home dot JSP you want to actually show the list of books which are there any database, but all actually I need to call IVR when it is going to home dot JSP you are not called Get our books method here. So what we can do we can redirect it to this method. So that again it is calling get our books methods and it is shown in the first page. So what I will do instead of four and just going to have it a redirect colon slash okay so this is the next step we go to Home dot JSP and I have to use expression language to retrieve the list of books. So let me copy it and I will just say apart from that list using expression language and trying to retrieve a list of books.
Next we need to go in for update from when I just use update for that it has to go it has to go to the book controller and it has to work on a page with one form field to get the idea So let me just add the method. So just like get mapping class at four, I'm going to have update from so the method I will update from. So when the mapping is update form it has to open update form that JC wherein you will be having a form with one form fields. So let me create update from the JSP and I will add the form. So this is my update form. Actually we are trying to get one book by ID okay get book by ID.
I don't need anything else I am not going in for method of course, update backprop let me just complete it and then I will explain it from home that JC when I click update from it will go to the controller and it will come to the stage from the space again it will go to the controller in the controller you are trying to retrieve the book. Then you are going to call another page We will be having your form wherein the booth details will be populated. So, let me just take this link and I will create a method in book controller. So, now just now I will just copy this and we are not using post mapping we are using get mapping only and it is update book. So, we need another form okay so, retrieve the book ID so, I just have to say record what is coming in say this book ID typing in the book ID and now here what I have to do book service dot get book by ID it is going to return a book object so it is book service dot get book by ID it is going to return a book object Now, this book object should be available for the next method also.
So what I need to do is attach this to a model. If it is available in the model, then it can be accessed via the other methods provided you have added the session attributes. So, let me just go up and add the session attribute at the rate session attributes. What is the dominant book because you're adding session attributes initially itself it lasts for a book or it will look for a book object. So, in the first method itself, in short form itself, I'm going to create a book object and I am going to give the domain name as model dot add attribute the dummy name is booked. So, the session attribute will be using only this book object.
It will not wait for spring to create the book object I hope you can understand. Your new book object is getting created here in this place and it is happening to the session attribute the same objects as I gained retrieve to that object, you are assigning the values and you are calling book service dot add book method. So now I have I'm here in this place. Now what I have to do model I don't have a model. So I need to add one more parameter here, comma Model Model. Now I'll just say model dot add attribute book the book which we have got from the database for that you are giving a dummy name book.
Now what the second attribute will be having the second attitude is going to have a new book which we are bought from the database. That book only should be shown as a form in the next stage. So here what I hope to do again I need to go in for update before. Okay now update performance that now I will go to this update book from the JC I have created it already. Now I need to change it here. Update books the method is forced and arbiters are updated.
Now, what about the value basic as I told you, now, the book object has come now I have to show the farm the book details. So for that I'm going to use value dollar book dot title same way I need to do all the in all the form fields. Let me do that the model attribute book I am trying to retrieve one property and assign it to the form field. What about book ID book ID should not be updated. So I want it as read only. So let me add this read only attribute.
Now this is that what is the action it is update book method is post. So in the book controller, I have to add a method and just copy this again. Pause mapping modules update book the methods book service dot update book. Now we are mapping only the book object the modified bukata can turn into this particular method and from here we are calling this book object or sorry update book method, can you see the link the link is between these two methods, a book object is retrieved over here the same book object has been hit by this method. So, what we are doing we are appending it to a session retrieving the book object in the fall I mean I'm taking all the properties and showing in the form fields the updation is happening once it is entering here from here we can update the book any database table.
So, this is that Nexus delete prop delete form also I need a form with one form field to get the book ID from that directly I can call what delete method so I will just copy this first. Okay, so here it is going to be DELETE FROM I need to create a delete That JSP page and then one method to delete the book okay. So here delete delete books and this is not taking model attribute it is going to take a request. So I need to copy this for delete method we are not passing the book object we are passing only the Id need to pass the book ID okay so I need a delete form also which is going to be similar top data scientists copy it and I will paste it delete So, the replicates This is done completely let me run the application first.
I have got my first page from the JSP you can see I've got the list of books which is shown as a list only episode if you want You can use jst l to show it in the form of a table add book already we have seen let me just click Update book I am trying to update the first book which is having an IDS 102 so let me just give it as 102 Let me click get book I have bought the update book form with all the values are 102 this alone I cannot modify you can just see this because it is read only JSP JSP Made Easy Okay, let me click Add book I need to go and change that up I mean button name. Now again it is taking me to home dot JSP we are saying that can you see JSP Medici shown over here.
Now let me click Delete book. I'll do the same ID 102 Click Get book. Okay the link I do not change it so it is just giving me an error, delete form the gspc here where it has to go just to go to delete and here it is delete and rerun the application. Now the application has started, I am in homepage. Let me click Delete book. I'll do the same ID 102 click Delete book Oh there is an error okay I have given a dispose method action equal to delete book.
Let me go into this book controllers see it so I will continue this part in the next session, but I want to give more tips of working with Spring and Hibernate. Thank you