Making Service Calls from a Controller2:40 with Chris Ramacciotti
With our first DAO and service coded, we're now ready to connect these layers with our web layer by making calls to the CategoryService from the CategoryController.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s2v5
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 firstname.lastname@example.org: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.
Okay it's time to completely close the gap between our controller and the database. 0:00 We're most of the way there already. 0:04 We work from our database back toward our user by going from database up to 0:06 hibernate up to DAO to service and now we need to get all the way to the controller. 0:11 As a reminder we configured our database using this data source bean. 0:16 And from app.properties we polled in specific values that configure this 0:20 data source as an H2 database, by flip back to app.properties here 0:25 I will see that our driver comes from that, H2 library. 0:31 Moving up from the database we get to hibernate our ORM. 0:36 We use this local session factory being to configure that. 0:40 So we got the database, we've got hibernate and 0:44 then we move up to the DAO layer. 0:47 Now, each entity class will contain its own DAO and 0:50 within that DAO will have one interface that is category DAO And 0:54 its implementation in this case CategoryDao input. 0:58 The next thing we did was create a service layer, and as is the case for the dao for 1:03 each entity object will have both a service and a service implementation. 1:09 This service layer is to provide a way for 1:15 various clients to access our data in our case our controller is that client. 1:16 The controller is going to control our web facing user interface. 1:22 So to close that gap between the database and 1:27 our controller let's go ahead and open CategoryController. 1:30 Will start by removing all the hibernate related code since our 1:35 DAO handles all of that. 1:38 So, we won't need to auto wire a session factory here and 1:40 we won't need to open a session factory here. 1:44 So, I'll just delete that. 1:46 We certainly won't need the criteria object here so I will delete that as well. 1:48 Now since our controller is going to speak to the service layer instead of directly 1:54 to the DAO layer, or as we had it in here before directly with Hibernate, 1:59 it's going to talk to the service layer so that's what I will need to 2:04 auto wire up here in place of auto wiring this session factory. 2:07 So let's auto wire a category service. 2:12 I'll just call it categoryService, cool. 2:16 And then we can use that category service to fetch the list of category objects in 2:19 our ListCategories method. 2:23 So we'll say categoryService.findAll, and that closes the gap for us. 2:26 In the next video, we'll fire up our application to verify that we've correctly 2:34 implemented a service DAO pattern. 2:38
You need to sign up for Treehouse in order to download course files.Sign up