Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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 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.
Okay it's time to completely close the gap between our controller and the database.
We're most of the way there already.
We work from our database back toward our user by going from database up to
hibernate up to DAO to service and now we need to get all the way to the controller.
As a reminder we configured our database using this data source bean.
And from app.properties we polled in specific values that configure this
data source as an H2 database, by flip back to app.properties here
I will see that our driver comes from that, H2 library.
Moving up from the database we get to hibernate our ORM.
We use this local session factory being to configure that.
So we got the database, we've got hibernate and
then we move up to the DAO layer.
Now, each entity class will contain its own DAO and
within that DAO will have one interface that is category DAO And
its implementation in this case CategoryDao input.
The next thing we did was create a service layer, and as is the case for the dao for
each entity object will have both a service and a service implementation.
This service layer is to provide a way for
various clients to access our data in our case our controller is that client.
The controller is going to control our web facing user interface.
So to close that gap between the database and
our controller let's go ahead and open CategoryController.
Will start by removing all the hibernate related code since our
DAO handles all of that.
So, we won't need to auto wire a session factory here and
we won't need to open a session factory here.
So, I'll just delete that.
We certainly won't need the criteria object here so I will delete that as well.
Now since our controller is going to speak to the service layer instead of directly
to the DAO layer, or as we had it in here before directly with Hibernate,
it's going to talk to the service layer so that's what I will need to
auto wire up here in place of auto wiring this session factory.
So let's auto wire a category service.
I'll just call it categoryService, cool.
And then we can use that category service to fetch the list of category objects in
our ListCategories method.
So we'll say categoryService.findAll, and that closes the gap for us.
In the next video, we'll fire up our application to verify that we've correctly
implemented a service DAO pattern.
You need to sign up for Treehouse in order to download course files.Sign up