Now that we have the session factory and the entity class, let's go and create a new program to add a new category to our database table. To do so, let's go and create a new Java class under a new package called program. So I'm going to call this as new class, but change the package to programs and this is going to be my first example program. So I'll call it as x 01. Let's call it as main and then also add a public static void Main. Click on Finish, we now have a program that's full screen this and then get rid of this to do now I want to save a new category into the database table.
Remember, we don't have the table yet. So let's create a category object by typing categories T one equals to new category imported from our own entity package, and says even dot set category name to that say coverages. And then I'm going to type here see one dot set description, let's say for example, say coffee, tea, etc. Notice I do not have an ID at this point in time. So if I print c one dot get category ID, it's going to give me a null value. I want hibernate to automatically generate the ID based on the strategy that we are given, which is called identity.
The first thing that we want to do is to get session factory so I'm going to type here factory equals to hibernate util dot get session factory, I have not created this variable of any data type. So that's why I'm getting an error. All I have to do is to press Command one on Mac Ctrl one on Windows, and it will automatically say create a local variable called factory. And when I press enter, based on the return type of get session factory, it will associate this variable with the type also automatically imports that so I press Enter. And now you can see that it has imported that created a variable using that, since we stored the factory in the service registry in the hibernate util, it is important that you close the factory at the end of this program, even if there was an exception, so I'm going to put a try with finally block.
And then inside the finally block, I'm going to say factory dot close, so that even if there were any exceptions, the factory gets automatically closed. Now in order to save the category into the database, we have to say session equals to factory dot open session. The factory dot open session returns a hibernate session, which represents a DB connection. Since I did not create the variable session with the data type, it's giving me an error. Like in the previous case, I'm going to press Command one, press enter and I got the session here and session being imported here as well. If you want to perform, insert, update, or delete, which are DML statements, you must put those statements in a transactional boundary Which means you have to begin a transaction and end the transaction by calling commit or rollback hybrid session provides a function called begin transaction which creates a transaction object.
So I can simply say session dot begin transaction, assign this to a variable called TX. press Command one to import and create the variable automatically. Now that we have our transaction variable, let's put a try catch block. And then inside the try catch block, let's try saving this. In order to save the category object. I simply say session dot persist, and then type C one which is a category object.
If there is successfully done I want to commit. If there is any error, I want to roll back. So if this is successfully saved, I want to tell the user that category object has been saved and type your new category saved with ID and then I type your C one dot get category ID remember earlier I mentioned that at this point in time, we have not set the ID. So if we say C one dot get category ID, I would have got none. But at this point in time, it has already been inserted into the DB, the newly generated ID has already been retrieved. And hibernate already has set the ID for C one.
Now all I'm doing is getting the ID. But in case if there was any problem, it will go to the catch block, rolls back the transaction. And then I type here System dot out dot print ln, there was an error while saving the new category, and if I want to give the message I can also say here, e dot get message. So let's save this and run the same. But just before running, we will go to the DB client here, connect to our database. Notice I don't have any table called categories.
So let's run this and check if it's gonna work. When I run this. You may see that there is a Insert statement, but I didn't see a CREATE TABLE command. And it's also giving me an error saying that table category's not formed. This is probably an error because of my hibernate util, get session factory here. And the property here is hibernate dot HBM to DDL dot auto.
I missed that part. I save this, close this, go back to the program and rerun that once more. And I should now see that there is a CREATE TABLE command which creates the table called categories and then inserts that it also gave me a message saying that new categories saved with ID one. Let's go to the DB client here and click on the refresh button over here. And you see that there is a categories table created. I click on this it shows me select star from categories.
I click on Run and you can see that the newly inserted category from the Java program appears over here.