1 00:00:00,830 --> 00:00:04,360 In the context of the basic CRUD operations in our application, 2 00:00:04,360 --> 00:00:06,430 we're only half-way there. 3 00:00:06,430 --> 00:00:10,030 We've discussed creating with a hibernate session save and 4 00:00:10,030 --> 00:00:12,680 reading with a hibernate criteria. 5 00:00:12,680 --> 00:00:17,150 Let's now turn to updating and deleting so that you have a more solid foundation for 6 00:00:17,150 --> 00:00:19,890 further research and experimentation with Hibernate. 7 00:00:21,570 --> 00:00:26,070 In both the update and delete scenarios we'll need a persistent object. 8 00:00:26,070 --> 00:00:28,910 What this means is that we'll have to have already fetched the object from 9 00:00:28,910 --> 00:00:33,520 the data base so in each of these cases we'll first find a contact by ID. 10 00:00:33,520 --> 00:00:35,590 Let me show you how that's done in hibernate. 11 00:00:35,590 --> 00:00:38,220 To keep our application class tidy I'll make another method for 12 00:00:38,220 --> 00:00:41,540 getting a contact with a certain ID and I'll include comments for 13 00:00:41,540 --> 00:00:43,310 what we're gonna do in this method. 14 00:00:43,310 --> 00:00:46,030 We'll do this right below the main method. 15 00:00:46,030 --> 00:00:51,120 We'll call it private static, it's gonna return a contact object and 16 00:00:51,120 --> 00:00:58,250 I'll say find contact by ID using a given int ID. 17 00:00:58,250 --> 00:01:01,830 So the first thing we'll do is open a session, this should look familiar. 18 00:01:01,830 --> 00:01:05,050 We'll retrieve the persistent object. 19 00:01:07,500 --> 00:01:08,620 Or null if it's not found. 20 00:01:11,140 --> 00:01:12,310 We'll close the session. 21 00:01:13,360 --> 00:01:16,680 And finally will return the contact object. 22 00:01:18,230 --> 00:01:21,650 Most of this we've already done before so I'll fill in those pieces now. 23 00:01:21,650 --> 00:01:25,180 To open the session, as you recall, we'll declare a session and 24 00:01:25,180 --> 00:01:27,410 we'll use the sessionFactory to open a session. 25 00:01:29,490 --> 00:01:32,830 This is the line that we'll come back to in just a moment. 26 00:01:32,830 --> 00:01:36,400 To close the session that's a simple call to session.close. 27 00:01:36,400 --> 00:01:41,160 And finally returning that object return contact. 28 00:01:41,160 --> 00:01:44,870 Speaking of that contact object, hibernate provides a simple way to retrieve 29 00:01:44,870 --> 00:01:49,620 a persistent object using its identifier and that is the session's get method. 30 00:01:49,620 --> 00:01:51,480 Here's how it looks. 31 00:01:51,480 --> 00:01:56,570 So into this contact variable we will assign session.get. 32 00:01:56,570 --> 00:02:01,320 We'll specify the name of the entity class and we will give it 33 00:02:01,320 --> 00:02:06,440 the integer identifier associated with the contact object that we're looking for. 34 00:02:06,440 --> 00:02:09,960 And that's all we need for the findContactById method. 35 00:02:09,960 --> 00:02:13,090 Now let's write a method that will take this contact object and 36 00:02:13,090 --> 00:02:15,400 persist any changes to the database. 37 00:02:15,400 --> 00:02:19,060 Let's create the method stub and comments for what we need to accomplish. 38 00:02:19,060 --> 00:02:23,280 I'll do that below the current method, private static void, 39 00:02:23,280 --> 00:02:27,910 we'll call it update and we wanna update the specified contact object. 40 00:02:27,910 --> 00:02:32,674 So again, we will open a session here we'll begin a transaction, 41 00:02:32,674 --> 00:02:36,587 since there are changes to be written to the database, 42 00:02:36,587 --> 00:02:39,921 we'll use the session to update the contact. 43 00:02:41,832 --> 00:02:47,620 We'll commit the transaction and finally we'll close the session. 44 00:02:48,820 --> 00:02:51,770 Now, all of this will likely look fairly intuitive and 45 00:02:51,770 --> 00:02:55,000 in line with the code that we've used to save an entity. 46 00:02:55,000 --> 00:02:58,510 In fact, the only difference is gonna be that instead of calling the save method, 47 00:02:58,510 --> 00:03:01,440 we'll be calling sessions update method which you 48 00:03:01,440 --> 00:03:04,450 probably could've guessed with your IDE code suggestions. 49 00:03:04,450 --> 00:03:06,530 So let's get this thing done. 50 00:03:06,530 --> 00:03:08,130 First we will open a session. 51 00:03:09,590 --> 00:03:15,120 Session = sessionFactory.opensession. 52 00:03:15,120 --> 00:03:20,680 We'll begin the transaction by using session.beginTransaction. 53 00:03:20,680 --> 00:03:23,360 Here's the one line that's different from saving a new entity, 54 00:03:23,360 --> 00:03:26,930 we'll say session.update the contact. 55 00:03:28,740 --> 00:03:30,710 Then we will commit the transaction. 56 00:03:31,975 --> 00:03:37,550 Session.getTransaction, that gets the open transaction that commit and 57 00:03:37,550 --> 00:03:40,350 finally we will close the session. 58 00:03:40,350 --> 00:03:42,830 Session.close. 59 00:03:42,830 --> 00:03:45,630 Now with that done let's pull it all together in the main method. 60 00:03:45,630 --> 00:03:48,730 So let me scroll back up here to the main method. 61 00:03:49,880 --> 00:03:52,450 What I'd like to do is update the contact that we saved 62 00:03:52,450 --> 00:03:57,490 at the beginning of this method, that is this contact object here. 63 00:03:57,490 --> 00:04:00,450 To do this we'll need to have its ID. 64 00:04:00,450 --> 00:04:04,750 But because this was generated by the database we don't currently have 65 00:04:04,750 --> 00:04:07,340 access to this integer ID in our source code. 66 00:04:08,440 --> 00:04:11,290 But hibernate has a solution for that. 67 00:04:11,290 --> 00:04:14,580 Whenever an entity is saved with a sessions save method, 68 00:04:14,580 --> 00:04:17,425 that save method returns the assigned identifier, 69 00:04:17,425 --> 00:04:22,680 whether its generated by application code or by the database, as is the case for us. 70 00:04:22,680 --> 00:04:28,180 Let's scroll down to the save method and update it to return the int id. 71 00:04:29,300 --> 00:04:35,925 To do that right here we will say, int id = session.save. 72 00:04:35,925 --> 00:04:39,500 Now the save method returns a serializable object so 73 00:04:39,500 --> 00:04:43,050 we'll need to cast that return value to an int. 74 00:04:43,050 --> 00:04:48,720 Wonderful, next we'll return the integer ID and add the return type. 75 00:04:48,720 --> 00:04:52,010 So at the bottom here let's return the I, and 76 00:04:52,010 --> 00:04:56,090 instead of making this a void method let's switch that to an int. 77 00:04:56,090 --> 00:04:59,830 So we now have access to the generated ID up in our main method. 78 00:04:59,830 --> 00:05:03,630 So I will scroll back up and alter my code there. 79 00:05:03,630 --> 00:05:09,450 So before the save I'll add an int ID = save contact. 80 00:05:09,450 --> 00:05:12,650 Let's create a log of outputs so that we can observe the progress of 81 00:05:12,650 --> 00:05:16,640 all of our steps and verify that our update indeed works. 82 00:05:16,640 --> 00:05:20,990 So this first display of our contacts is gonna be displaying a list of contacts 83 00:05:20,990 --> 00:05:22,890 before the update. 84 00:05:24,150 --> 00:05:27,980 And the next thing that we're going to do is get the persistent contact. 85 00:05:30,810 --> 00:05:32,306 We'll update the contact. 86 00:05:32,306 --> 00:05:39,440 We'll persist the changes and 87 00:05:39,440 --> 00:05:44,520 then we'll display a list of contacts after the update. 88 00:05:45,950 --> 00:05:48,480 Now let's back fill a couple lines of code for 89 00:05:48,480 --> 00:05:52,826 retrieving the contact, altering it, then persisting those changes. 90 00:05:52,826 --> 00:05:57,450 So for getting the persisted contact we will say contact, 91 00:05:57,450 --> 00:06:03,590 I'll call it C in this case, equals findContactById, and here's the integer 92 00:06:03,590 --> 00:06:08,660 Id that will have been generated when we persisted this initial contact. 93 00:06:08,660 --> 00:06:14,680 So we'll hit the database again and grab the result as a contact object. 94 00:06:14,680 --> 00:06:17,500 Then we can alter the contact just as we would any 95 00:06:17,500 --> 00:06:19,910 other Java object by using a setter. 96 00:06:19,910 --> 00:06:21,710 I'll alter the first name. 97 00:06:21,710 --> 00:06:25,900 So under Update the contact, I'll say c.setFirstname and 98 00:06:25,900 --> 00:06:28,210 I'll set the first name to Beatrix. 99 00:06:30,350 --> 00:06:33,340 And to persist those changes we'll call the update method that we wrote 100 00:06:33,340 --> 00:06:36,869 just a bit ago, update(c). 101 00:06:38,270 --> 00:06:41,090 Now let's give ourselves a couple lines of output so 102 00:06:41,090 --> 00:06:44,410 that we can monitor our progress along the way. 103 00:06:44,410 --> 00:06:49,550 So right here I'll do a System.out.print f and I will say that this, 104 00:06:51,150 --> 00:06:55,130 give myself a couple new lines, this is before the update. 105 00:06:57,670 --> 00:07:01,640 Want those to stand out so I'll surround that update by a couple new line 106 00:07:01,640 --> 00:07:05,970 characters, now let's give ourselves a couple lines of out put before and 107 00:07:05,970 --> 00:07:08,810 after the update so we can monitor those changes as well. 108 00:07:08,810 --> 00:07:14,142 I'll copy that before and after this 109 00:07:14,142 --> 00:07:19,138 persist so this will be updating. 110 00:07:21,206 --> 00:07:25,837 And then we'll display a message that says update complete. 111 00:07:29,776 --> 00:07:33,990 And finally, let's display our list of contacts after the update. 112 00:07:33,990 --> 00:07:35,940 I'm going to copy these two lines of code, 113 00:07:35,940 --> 00:07:38,320 since we'll be doing essentially the same thing. 114 00:07:38,320 --> 00:07:42,370 We'll paste it down here and I will change this to after 115 00:07:43,980 --> 00:07:48,290 and we want to do the same thing for this second line of code which will again fetch 116 00:07:48,290 --> 00:07:53,100 all contacts, create a stream out of it and on each one of those contact 117 00:07:53,100 --> 00:07:58,020 objects we will pass that contact object to the system.out.println method. 118 00:07:59,210 --> 00:08:00,930 And that's all there is to it. 119 00:08:00,930 --> 00:08:04,720 Let's run our application to verify that everything is working. 120 00:08:04,720 --> 00:08:06,110 Right click and choose run. 121 00:08:07,570 --> 00:08:14,760 And if you look closely you will see that our first run through the output here 122 00:08:14,760 --> 00:08:20,130 has our last contact, that is the one with the ID of four as a first name of Chris. 123 00:08:20,130 --> 00:08:25,720 And then we have our update in which this hibernate query is run, 124 00:08:25,720 --> 00:08:27,870 that is sent to the database. 125 00:08:27,870 --> 00:08:29,930 Upon completion, we have our update complete, 126 00:08:29,930 --> 00:08:31,740 and here's our list after the update. 127 00:08:31,740 --> 00:08:33,020 Let's see if it worked. 128 00:08:33,020 --> 00:08:35,980 Down at the bottom we have our contact with the ID of four, 129 00:08:35,980 --> 00:08:40,740 whose last name has now been successfully persisted to the database. 130 00:08:40,740 --> 00:08:42,870 As Beatrix, first name rather. 131 00:08:44,060 --> 00:08:48,370 So as a recap of what we just did, we retrieved a persisted object that we 132 00:08:48,370 --> 00:08:52,260 wanted by knowing it's ID and using the session's get method. 133 00:08:52,260 --> 00:08:55,430 Then we altered the POJO using a basic setter. 134 00:08:55,430 --> 00:08:59,030 Finally we called the session's update method to persist those changes. 135 00:08:59,030 --> 00:09:03,250 And just like that our changes were persisted to the database. 136 00:09:03,250 --> 00:09:04,180 Pretty cool stuff! 137 00:09:05,780 --> 00:09:09,370 You might be wondering at this point why I haven't finished what I promised at 138 00:09:09,370 --> 00:09:11,130 the beginning of this video. 139 00:09:11,130 --> 00:09:13,160 How do you delete a persisted object? 140 00:09:14,230 --> 00:09:15,210 I haven't forgotten though. 141 00:09:16,700 --> 00:09:20,840 What I'd rather do is have you take a look at what you've learned from this course 142 00:09:20,840 --> 00:09:23,740 and extend it to this one additional operation. 143 00:09:23,740 --> 00:09:25,870 The delete operation. 144 00:09:25,870 --> 00:09:29,900 You can try some things using your IDE's code suggestion tool or 145 00:09:29,900 --> 00:09:33,440 you can hit up the online hibernate docs that I've linked to in the teachers notes. 146 00:09:34,630 --> 00:09:38,510 Either way, be sure to give yourself plenty of output to verify successful 147 00:09:38,510 --> 00:09:40,120 functionality. 148 00:09:40,120 --> 00:09:43,320 Though you'll get the most out of accomplishing this yourself, 149 00:09:43,320 --> 00:09:46,220 I've made my solution available on GitHub. 150 00:09:46,220 --> 00:09:49,220 If you want to see it, Go to the online repo or 151 00:09:49,220 --> 00:09:51,450 check out the revision named final. 152 00:09:51,450 --> 00:09:52,340 Now have at it.