Hibernate – Annotations
Annotation in JAVA is used to represent supplemental information. As you have seen @override, @inherited, etc are an example of annotations in general Java language. For deep dive please refer to Annotations in Java. In this article, we will discuss annotations referred to hibernate. So, the motive of using a hibernate is to skip the SQL part and focus on core java concepts. Generally, in hibernate, we use XML mapping files for converting our POJO classes data to database data and vice-versa. But using XML becomes a little confusing so, in replacement of using XML, we use annotations inside our POJO classes directly to declare the changes. Also using annotations inside out POJO classes makes things simple to remember and easy to use. Annotation is a powerful method of providing metadata for the database tables and also it gives brief information about the database table structure and also POJO classes simultaneously.
Setting up the Hibernate Annotations Project
It’s recommended to set up the Maven project for hibernate because it becomes easy to copy-paste dependency from the official Maven repository into your pom.xml.
Step 1: Create Maven Project (Intellj)
Go to next and name a project and click to finish.
Step 2: Add the dependency to the pom.xml file
After setting up a maven project, by default, you get a POM.xml file which is a dependency file. POM stands for project object model, which allows us to add or remove dependency from 1 location.
The project structure and pom.xml should look like this. Now, add hibernate and MySQL dependency to use annotations to create a table and to use HQL(hibernate query language).
Make sure you add dependency and it should look like the above file.
Step 3: Add hibernate.cfg.xml file for database parameters
We use the hibernate.cfg.xml file to provide all related database parameters like database username, password, localhost, etc. Make sure you make the hibernate.cfg.xml inside the resource folder
The file should look like above
Step 4: Add POJO and main classes for working with the functionality
Here are some annotations used in our POJO specifically for hibernate-
Use of annotations
|@Entity||Used for declaring any POJO class as an entity for a database|
Used to change table details, some of the attributes are-
|@Id||Used for declaring a primary key inside our POJO class|
|@GeneratedValue||Hibernate automatically generate the values with reference to the internal sequence and we don’t need to set the values manually.|
It is used to specify column mappings. It means if in case we don’t need the name of the column that we declare in POJO but we need to refer to that entity you can change the name for the database table. Some attributes are-
|@Transient||Tells the hibernate, not to add this particular column|
|@Temporal||This annotation is used to format the date for storing in the database|
|@Lob||Used to tell hibernate that it’s a large object and is not a simple object|
This annotation will tell hibernate to OrderBy as we do in SQL.
For example – we need to order by student first name in ascending order
These are some annotations that are mostly used in order to work with hibernate.
Student.java (POJO class)
Main.java (Action class)