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, 0:00 update and delete Gif and category data. 0:04 What amounts to the current functionality of Gif lib, 0:07 we'll need to make changes to all layers of the application. 0:09 That is, we'll need to code the ability to save category objects in our DAO layer. 0:13 Add access to that functionality via the service layer and 0:19 add a method to our category controller that will call that service functionality. 0:22 Even more, we'll need an HTML form that allows a user to initiate the action 0:27 of adding a category. 0:32 Let's start with our database and work our way to the user. 0:34 That is, let's code the DAO, then the service, 0:37 followed by the controller and finally the HTML. 0:40 Because our DAO interface in implementing class already have a save method, 0:44 our work here is just about adding the hibernate code to perform those tasks. 0:48 So let's open CategoryDaoImpl. 0:52 Down in the save method, let's drop some comments in there to remind 0:56 ourselves of the general process of writing to the database. 0:59 So first, 1:02 we'll open a session it's a hibernate session, then we'll begin a transaction. 1:03 Next, we'll save the category that we received in the method as 1:10 a parameter value. 1:13 Next, we'll commit that transaction and finally, 1:16 we'll close the hibernate session. 1:21 Now we haven't used transactions in this course yet, but 1:24 you saw them in Hibernate Basics. 1:26 As a reminder, transactions ensure that related edits 1:29 all complete before changes are confirmed or committed to the database. 1:32 Okay, here we go. 1:37 So first, 1:38 open a session will declare that hibernate session I'll just call it session again. 1:39 And from the SessionFactory I will open a session. 1:44 Next, we can use that session we just created to begin a transaction. 1:49 With that transaction started, 1:54 we can again use that session to save that category we received. 1:57 And since we've now saved the category with the hibernate session, 2:02 let's commit that transaction. 2:05 So I'll use session dot get transaction dot commit. 2:07 And finally, let's not forget to close the hibernate session, 2:12 session.close and that's it for CategoryDaoImpl. 2:16 Now on to the CategoryServiceImpl, 2:22 so I will open that which is in the service package. 2:24 In this class, we already have this @Autowired categoryDao. 2:28 So in our save method, we'll simply call upon that to do its saving. 2:32 So categoryDao.save and then the category object that this service method received. 2:37
You need to sign up for Treehouse in order to download course files.Sign up