Hibernate Annotations

I always try to write things in such a manner that everyone will be able to read and learn things in one sitting. This will also help you to get overall idea of Hibernate and using Hibernate with annotations.

Hibernate annotations is all about mapping or relationship in between bean class and database.

Below is the best example to understand the same the Hibernate annotation with bean class.

User class highlighted in red is having @Entity and @Table annotations to map the User class with usr table.

@Entity annotation will mark class as an Entity.

@Table annotation will map the class with provided table. name attribute can be use to map class name with database table name. It will by default take table name as class name if not provided.

@Id annotation is an Entity identifier

@GeneratedValue use to generate value. Below are the ways to generate value. This mainly helps to create unique primary key.

Hibernate Project Setup

Lets create a sample project using Hibernate annotations. The process is similar to what we earlier followed defining bean class mapping in old user.hbm.xml approach. 

user.hbm.xml will never come in picture if we are using Hibernate annotations.

Pre requisite before starting hibernate development is to have Java, MySQL and Eclipse installed in local.

Below Example will help us to create Hibernate project using Eclipse to insert data in MySQL database.

1. Let’s create a maven project using Eclipse.

Click on File in top navigation and select New and than click on maven Project.

File β†’ New β†’ Maven Project

Below window will appear, no changes are required and click on Next

2. Wait for sometime or archetype to get appear on screen. Select below highlighted archetype Artifact Id as maven-archetype.quickstart and Group Id as org.apache.maven.archetypes.

click on Next after below inline selection.

3. provide the Group Id and Artifact Id as mentioned below. Click on Next.

4. Copy paste below dependencies for hibernate and mysql under dependencies section in pom.xml.

5. Create a User.java inside com.javadoubts.PractiseHibernate package which will be persistence object to save data in database. Assign all the respected annotations to Class and its variables.

6. Create resource folder under below highlighted hierarchy.

Very important Step:
right click select Build Path option and than select Use as Source Folder.

7. Create hiernate.cfg.xml file to provide info related to database connection as mentioned below.

Load User bean class at line number 22 which will load class and its mapping.

9. Copy paste below code to App.java class. This class will help to load Hibernate configuration to connect with database and bean class having User bean class mapping with table and variable with column mapping.

Below code is fetching user data with the help of bean class mapping wih table without writing SQL query.

10. Create a table usr in MySQL using below query.

create table usr(userId int NOT NULL AUTO_INCREMENT, firstName varchar(20), lastName varchar(20), age int, primary key(userid));

11. Run App.java main function as Java application will fetch record from database as shown below.

Cheat Sheet

Below is the quick walk though of the steps we performed above to insert data in database.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

πŸ“ Blogs

πŸ“§ Imran Khan