12. Integrating Spring with Hibernate - II

23 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.