Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Saving Entities2:44 with Chris Ramacciotti
In this video we begin our work of persisting new category entities to the database. We start by coding the DAO, in which we use a transaction to enclose the operation of saving a new category.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s3v2
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.
As we begin giving our users the ability to create, read,
update and delete Gif and category data.
What amounts to the current functionality of Gif lib,
we'll need to make changes to all layers of the application.
That is, we'll need to code the ability to save category objects in our DAO layer.
Add access to that functionality via the service layer and
add a method to our category controller that will call that service functionality.
Even more, we'll need an HTML form that allows a user to initiate the action
of adding a category.
Let's start with our database and work our way to the user.
That is, let's code the DAO, then the service,
followed by the controller and finally the HTML.
Because our DAO interface in implementing class already have a save method,
our work here is just about adding the hibernate code to perform those tasks.
So let's open CategoryDaoImpl.
Down in the save method, let's drop some comments in there to remind
ourselves of the general process of writing to the database.
we'll open a session it's a hibernate session, then we'll begin a transaction.
Next, we'll save the category that we received in the method as
a parameter value.
Next, we'll commit that transaction and finally,
we'll close the hibernate session.
Now we haven't used transactions in this course yet, but
you saw them in Hibernate Basics.
As a reminder, transactions ensure that related edits
all complete before changes are confirmed or committed to the database.
Okay, here we go.
open a session will declare that hibernate session I'll just call it session again.
And from the SessionFactory I will open a session.
Next, we can use that session we just created to begin a transaction.
With that transaction started,
we can again use that session to save that category we received.
And since we've now saved the category with the hibernate session,
let's commit that transaction.
So I'll use session dot get transaction dot commit.
And finally, let's not forget to close the hibernate session,
session.close and that's it for CategoryDaoImpl.
Now on to the CategoryServiceImpl,
so I will open that which is in the service package.
In this class, we already have this @Autowired categoryDao.
So in our save method, we'll simply call upon that to do its saving.
So categoryDao.save and then the category object that this service method received.
You need to sign up for Treehouse in order to download course files.Sign up