Deleting an entity is very easy. All you have to do is to retrieve the object from the DB and then call the session dot delete function. However, permanently deleting a record is not a general practice. Instead, we use something called soft delete where the record exists, but with a flag of something like non existent for example, if an employee quits the job, you don't want to delete his record, but instead you will mark that as an inactive employee. But to show you how the Delete works, let's go create a new Java class here with a package name of zero for class name is me. And I will also add this main function and I'm going to fullscreen the thing.
Also I'm going to paste this snippet which creates a session factories session begins a transaction commits, the transaction closes efficient and close the factory. So in order for me to import it, I'm going to press Command Shift Whoa, and make sure that I select from our hibernate session. And everything is okay now. So this is where I'm going to first retrieve the object. So it says session dot get category dot class and the ID happens to be one. I'll also import this category from my own entity package, assign this to a variable called C one create the variable called C one.
Now, in case if this ID doesn't exist, for example, if I have already deleted this and try to run this program, again, deleting a non existing entity will cause an exception. So it is better to check if c one not equals to null. Then I'm going to say session dot delete, and then supply c one. And I'll also give a message saying that deleted successfully. Save this around the same you'll see that it's going to send an SQL delete statement and It also gave me a message here that deletes successfully. If you're wondering why I got this message later, because the actual deletion happens when you do a commit, not when you call the Delete, when you call the session dot delete Steven hybrid simply records that and mark this one to be deleted and then it actually sends the SQL delete statement over here.
If I rerun this once more, you will see that there is no error as well as there is no message called deleted successfully. There is also no SQL delete statement file.