Hibernate Many to One Mapping Annotation

When a multiple class entities mapped to Single class entity is called as hibernate many-to-one mapping annotation approach.

In many-to-one mapping, the user and address entity are directly connected to each other. For example, in any application, multiple user entities can have single address entity.

The fetching of multiple user records can get single address record from the database.

This article will give you an overall idea on how many-to-one hibernate annotation works. It will also help you to answer most of the practical interview questions.

As part of this discussion, we will be creating sample Hibernate program to select, insert and delete data in MySQL database.

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

Below Example will help 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 resource folder under below highlighted hierarchy.

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

6. Create User.java file to provide mapping of User class and MySQL user_table table.

Create mapping for class variables and table columns.

It will also require to create a mapping of User.java class with Address.java as highlighted below red in color.

@Entity is to convert bean in to an entity.

@Table annotation helps us to map current bean as table. name attribute help us to map bean class name with MySQL table name. name attribute is not required if class name and table name is same.

@Column annotation helps to map class variable and MySQL column name.

@id represents to unique ID attribute in class map to the primary key of the database table.

@GenerateValue helps to generate unique id for given column.

@ManyToOne annotation will map multiple users to single Address entity.

7. Create Address.java file to provide mapping of Address class and address_tables MySQL table.

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

Load User.java and Address.java file using mapping tag as mentioned at line number 25 and 26.

9. Copy paste below code to App.java class. This class will help to load Hibernate configuration and Hibernate config with load User hbm file where we have written User class variable and column mapping.

Below code snippet to get, insert user and address records

10. Go hibernate.cfg.xml and update below line to create and run program to create table.

<property name=”hbm2ddl.auto”>create</property>

Please go back to MySQL and check if both the tables got created in MySQL with name address_tables and user_table.

If table didn’t get create, please run below script in MySQL editor or command prompt.

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

create table address_tables(id int NOT NULL PRIMARY KEY, addressline varchar(20), city varchar(20), pincode int);

If table gets created in MySQL, please change below line value to update as mentioned below in hibernate.cfg.xml:

<property name=”hbm2ddl.auto”>update</property>

Re-run the program again below print below output.

OUTPUT:

Please find link to download source code for many-to-one annotation mapping.

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