Updating Categories: Part 210:19 with Chris Ramacciotti
In the second of two parts we finish the update action of category entities. This video shows the necessary controller code for reusing the same form template for both adding and updating categories, as well as an update to our category DAO to prevent an updated category from being added as a new category.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s5v6
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse/giflib-hibernate.git
To update your local repository to match a video's starting point, you can use the
git checkout command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
I mean the category controller now is that we can add those model 0:00 attributes that are going to allow us to reuse the category form template both for 0:02 adding a new category as well as for adding an existing one. 0:07 We'll need to do this for the for new category method 0:12 which is the method I'm in right now as well as the form edit category method. 0:15 So I'll do it for one then repeat it for the other, first the new category method. 0:20 We'll need to add three attributes here 0:27 that are related to the ones that we added to the category form template and 0:29 those are the action, heading, and submit attributes. 0:34 So let's do those now. 0:37 We'll model.addAttribute the first one action And, this is going to be 0:39 the action attribute for the form that's used to add a new category. 0:45 The action attribute there, is just categories. 0:51 That form is going to post to this URI right here, /categories. 0:54 The next attribute that we need to add to the model is the heading attribute. 1:00 Model that addAttribute and we'll say heading and 1:04 for new category, we want that heading to simply say, New Category. 1:09 You can change that if you like. 1:13 And the last one is going to be the text to use for the submit button, and 1:16 we call that in the template. 1:20 Simply submit. 1:22 I'll use add here. 1:25 Again, you could use text of your choosing. 1:27 Now let me copy and paste these to the form edit category method and 1:30 tweak it from there. 1:33 So I will take these three lines of code. 1:35 And paste it into the form edit category method. 1:38 Right here. 1:41 Now for the form action attribute it's not going to post to the slash categories Uri. 1:43 It's going to post to slash categories slash. 1:48 Whatever the id happens to be. 1:51 So we'll use the string dot format function to do that. 1:54 String.format. 1:58 And what I want to substitute in for this placeholder here is 2:02 the categoryId that was received in this controller method. 2:06 CategoryId right here which will have come from the URI itself. 2:10 Next is the heading to display instead of New Category, I'll say Edit Category. 2:16 And finally the Submit button, the text I'd like to display there is Update. 2:21 Again, you could choose your own values if you like. 2:26 Now before we move on to coding the actual update, 2:30 let's make sure our form is rendered properly. 2:32 Both when editing an existing category and when adding a new category. 2:35 So let's reboot our app. 2:39 Let me pull this panel back up. 2:41 I'll stop my instance there, and I will rerun that boot run task. 2:43 Looks like everything compiled correctly. 2:51 And the application has successfully started, so let's switch to Chrome. 2:55 So I'll go to the Categories page first. 2:59 And let's look at the New Category form. 3:01 So here, cool, looks like it says New Category, I scroll down. 3:05 The button says Add, just as I wanted it to, and by inspecting the form 3:09 I can see that the form's action attribute is /categories. 3:15 Which is where I want a request to post when adding a new category. 3:21 Cool. 3:25 Now let's go to the Edit form of any one of the categories that are present. 3:26 So I'll click on Categories and let's go to the Edit form for Java. 3:31 Hey look at that, the heading now says Edit category 3:36 even though we're reusing the same time leave template. 3:39 If I scroll down a bit, 3:42 there we go instead of Add on the edit form it says Update. 3:44 Now let's see what the form's action attribute is, I'll inspect this input. 3:48 Just so i can get at this form element. 3:53 Cool, it's going to post to slash categories slash two. 3:56 Which is the ID of this category. 3:59 And hey, speaking of the ID, I see that hidden input here has a value of two. 4:02 That was our ID that came from the category entity. 4:09 So with all of that in place, let's jump back to the controller and 4:14 tie up our work. 4:18 In our Update Category method, 4:21 we'll want to do mostly the same things as we did in the Add Category method. 4:23 Let me hide this panel here. 4:27 And I see that method right at the bottom of my screen 4:30 here is all scroll up to reveal that Update Category method. 4:32 Cool. 4:36 So in here we'll want to make sure that we catch any validation errors and if so 4:37 redirect back to the edit form. 4:42 And if validation passes we'll save the category and 4:44 redirect with a flash message. 4:48 So all start by copying and 4:50 pasting that code that came from the Add category method. 4:51 Let me scroll down here cuz it's right below. 4:55 And I'll copy all of this code right here and 4:57 we'll make the appropriate changes up above. 5:02 So I'll copy that code and I will paste it right here. 5:05 There we go, cool. 5:12 Now We've got a bunch of compiler errors here and 5:13 that's because we havent added the proper parameters to our controller method yet. 5:16 Let's do that now. 5:20 First, we're missing a category parameter here, 5:22 nad that will come from the form submission. 5:24 So we can grab that and 5:27 Spring can reassemble that data by including it as a parameter here. 5:29 And remember we can trigger validation by including that 5:34 valid annotation just like that. 5:37 So we've resolved that category compiler error the next thing 5:41 We need to resolve is the result and that if you recall is the BindingResult. 5:44 So BindingResult result. 5:49 There we go that resolves that compiler error, and finally the redirectAttributes. 5:55 Let's add that as a parameter RedirectAttributes and 6:00 I will name it the same thing. 6:04 Redirect attributes. 6:06 Sweet! 6:07 All compiler errors in that method have been resolved. 6:08 But, we still have a couple changes to make. 6:12 First, if validation fails down here, we don't want to redirect to 6:14 the add form, but rather the edit form for this particular category. 6:19 So we'll need to redirect to /categories/ whatever the id is /edit. 6:24 I'll use the string.format method to do that. 6:30 So string.format, and I'll enclose this entire String in parentheses. 6:32 I will redirect/categories/%/edit. 6:38 And I want to replace that placeholder with the category category id. 6:44 So that's category.getId. 6:51 Now here you might think 6:53 can't I just use category id instead of category.getId. 6:58 Well as it stands, no you can't, 7:02 because you haven't included it here as a path variable. 7:05 But you certainly could add a parameter as a path variable here and 7:09 capture that categoryId parameter value, and use that in place of category.getId. 7:13 Okay, the next change that we need to make is that our FlashMessage Shouldn't say 7:20 that the category was successfully added, but 7:24 rather that it was successfully updated. 7:27 So I'll just change that to say updated and finally it's time to test the update. 7:30 So let's reboot our app Let me kill that. 7:36 Rerun the boot run task. 7:40 Everything compiled successfully, and the application started. 7:45 So let's go back to Chrome. 7:52 I'm gonna hide this inspector here. 7:53 Let's go back to the category index. 7:55 Cool. 7:59 Now I'm going to choose to edit the first category, technology. 7:59 Let's just edit this by making it plural. 8:04 Update. 8:08 Hey, look at that, category successfully updated. 8:08 But wait. 8:12 If you look closely you'll see that a brand new entry was made. 8:13 So that now you see both. 8:18 The old one technology, and the new one technologies. 8:20 That's no good. 8:24 Now, the problem here is that we're using the same hibernate save method that we use 8:26 for adding a new category. 8:30 Here, let me show you. 8:32 I'm gonna switch back to IntelliJ and open category DAO imple. 8:34 By looking at the list of methods here, we see that our DAO imple offers only 8:40 one Save method here, and it is this one save method that calls upon 8:45 the same Hibernate functionality to persist the entity to the database. 8:50 This method in Hibernate, this save method, always creates and 8:56 persists a new entity. 9:00 If we want Hibernate to be smart about creating versus updating, 9:03 we can instead call the saveOrUpdate method. 9:07 This method will determine whether or 9:13 not the category entity has been persisted and proceed accordingly. 9:15 If it has already been persisted, it will update. 9:19 Otherwise, it will insert a new one. 9:22 So with that change made, let's try again. 9:25 Let's stop the app, and let's rerun that run that boot task. 9:27 All has compiled and, the application has started. 9:34 So let's try that again. 9:40 Okay, let's edit Hibernate. 9:43 And let's change hibernate to say Hibernating. 9:45 And I will click Update. 9:49 It says, category successfully updated and I see that the existing one 9:51 was indeed updated to hibernating and there is not a new entry present. 9:56 So it's working exactly as We intended it to. 10:02 Hey, you're really making wonderful progress on these credit operations. 10:07 We're not done yet though. 10:11 To put what you've learned into practice, 10:12 next I'll ask you to code the updating of gifts yourself. 10:14
You need to sign up for Treehouse in order to download course files.Sign up