Coding the GIF DAO4:47 with Chris Ramacciotti
This video presents the code needed in the GifDao interface and implementation.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s5v3
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.
We'll start the DOA for GIFs by creating the interface. 0:00 And I'll create that in the dao package here, right click, New > Java Class. 0:03 I'll drop this down the Interface and choose GifDao. 0:07 Again, we'll include similar methods as we did for the categories. 0:14 In fact, these will be the same as the service interface except that we won't 0:17 need the multipart file parameter in the save method. 0:21 So I'll just copy and paste those from this service. 0:25 So I'll copy these, paste them here, hit OK. 0:29 I will not need that multipart file parameter there and 0:34 I can delete that import as well. 0:40 No need to import libraries that go unused. 0:44 Well, that was quick. 0:47 Let's create the implementation. 0:48 Create a new Java class, GifDaoImpl, awesome. 0:51 We'll mark this as a Repository and 0:59 we'll have it implement the GifDao interface. 1:03 Excellent. 1:09 Now let's implement all the methods of the interface. 1:10 There we go. 1:15 And now that we're coding the layer closest to our database, 1:17 it's time to invoke that Hibernate code. 1:20 We'll start by auto-wiring a Hibernate SessionFactory. 1:22 Let's do that at the top. 1:25 So we will auto-wire private SessionFactory. 1:27 We'll call it sessionFactory. 1:34 Next, let's go ahead and code the implementations for 1:36 each one of these methods while we're here. 1:39 For all of these methods we'll need to open a session, do some things, 1:41 then close the session. 1:44 If you'd like to drop that code into every method, that's what I'm going to do here. 1:46 So in the findAll method we'll open the session 1:50 using the sessionFactory.openSession. 1:55 Cool. 2:00 And then before I'm done with everything, I'm going to have to close this session. 2:03 Now I'm gonna do this in every single method. 2:08 So I'm gonna copy and paste this so I don't have to type and type again. 2:10 Definitely we'll have to fill in the spaces between the open session and 2:18 the close session with a bunch more code, but we don't have to retype that code. 2:22 So now for the rest of the findAll method, I'll define a list of GIFs to return. 2:27 So let's do that right here. 2:31 We'll say a list of Gif objects, cool. 2:33 I'll call it gifs, and I'll create a criteria object, 2:39 createCriteria, based on the Gif class. 2:45 And I want all of them, so I'm gonna call that list method. 2:48 Now, don't forget if you want to suppress this compiler warning, 2:51 you certainly can with the SuppressWarnings. 2:54 That's an unchecked assignment. 2:57 To finish this method, let's not forget to return that list that 3:00 we got back from the database instead of just returning null. 3:04 Oops, that's gifs, not gif. 3:07 Cool. 3:09 As for the findById method, after doing this one you should be able to do 3:11 the same for the category DAO, so here we'll use the session's get method. 3:15 So the GIF that we want back comes from session.get and 3:20 we want something from the Gif class. 3:25 That's the entity that we want to fetch. 3:29 And the ID that we receive is the thing that identifies the entity that we 3:31 are interested in, which is returned and stored into gif, so let's return that. 3:35 And next is the save method, which we're particularly interested in here. 3:43 Since this is a write operation., we'll use a transaction. 3:49 So we'll do session.beginTransaction. 3:53 Then we'll save the GIF, session.save, 3:58 that gif entity that we received as a parameter value. 4:02 We'll grab that transaction and we will commit it before closing the session. 4:06 And the delete method is almost identical except that we'll call the session's 4:14 delete method instead of the save method. 4:17 So I know, I'll just copy that code, paste it down here, 4:19 and instead of saving, was delete. 4:24 Cool. 4:27 All right, now that our service and DAO exist and are fully coded, let's 4:29 pop back to the service implementation and make sure the DAO is imported. 4:33 So back in the service implementation, we can import this now. 4:37 And that will resolve all the compiler errors that we were seeing before. 4:43
You need to sign up for Treehouse in order to download course files.Sign up