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
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
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.