Now that the project has been set up, let's close all this minimize our project. And let's add a new Java class under SRC main Java, which is going to be an entity class representing eventually the table data. So I'm going to call this as a category. So I go to new class, and then add a package name here called dot entity, and then call the class name here as category. This is a plain ordinary Java class or pojo. Let me increase the font size a bit.
Here, let's assume that we have a category ID, category name and a description of the category. So for representing the same I'm going to create few variables, private integer, category ID, private string, category name, and private string description will also add a default country struktur I can do so by pressing Control space and then look for the constructor. Get rid of the to do here. Also add the getters and setters for all these so that the private members are accessible outside of this class, I go to source menu, generate getters and setters, select all of them so that getters and setters are selected over here, I say okay. Also, I would like to add the to string function. So when I get a category object, I can print it out.
So I go to the source menu, generate to string already all the members have been selected, I click on OK to generate the two string function. So we want hibernate to create tables corresponding to our entity classes. We can do that by mapping this category dot java to a categories table and mapping these variables to appropriate column names of our table. Now hibernate will read this information and then automatically create the tables Update the tables if needed. To do so, first we have to add here at the rate entity. And then at the rate table name of the table we want this to be a map is categories, I also have to do an import, I can press Command Shift or on Mac or Ctrl Shift or on Windows to get that automatically imported.
Whenever there is a name conflict between multiple classes from different packages. This dialog box comes and then I can choose this from the list I say next. And then Java x persistence table I say finish accurate entity just gives an identity for this particular class that it is a special class for hibernate, so hibernate should manage. Also in the absence of accurate table, the accurate entity annotation maps this class to a table with the same name. So since we wanted the table to be in plural, I had to use a different annotation called at the rate table. Now in case if you're feeling names are different than the column names.
You can also map them by adding at the rate column name equals to category underscore ID. Similarly, I can say here at the rate column name equals to category underscore name for description, I'm not going to mention that, by default, hibernate will automatically map all the fields to appropriate column names. In this case, it will map this string description field to a column called description. So once again, in order to import the address column, I'm going to press Command Shift O, and automatically it adds that to my imports. One last thing that we have to do here is to map one of these three fields to a primary key of the table. Now generally, the IDS like category ID or product ID are going to be mapped for that.
And all I have to do is to say at the rate ID and import that from Java x persistence package, we can also have hibernate to automatically generate the IDS based on certain logics. For this, I may have to add another annotation called at the rate generated value. And then I say here, strategy equals two. There are four strategies as you can see auto identity sequence and table. Identity means the column corresponding to this will have an auto underscore increment feature. So I'm going to select it and save this now our category entity class is ready