One To Many and Many to One XML Mapping
This blog will help you to get overall idea of Hibernate one-to-many and many-to-one mapping with detailed example.
When a class entity is mapped to another multiple class entities is called as one-to-many mapping.
When a multiple class entities mapped to Single class entity is called as many-to-one mapping.
one-to-many mapping is opposite of many-to-one mapping.
In one-to-many mapping, the user and address entity are directly connected to each other. For example, in any application, a single user can have multiple addresses.
The fetching of user records will also fetch all related multiple addresses records from the database.
This article will give you an overall idea on how one-to-many hibernate xml 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.
- 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 and Address.java inside com.javadoubts.PractiseHibernate package which will be persistence object to save data in database.
To create one-to-many mapping, it is required to create Address class List variable in User.java class.
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 user.hbm.xml file to provide mapping of User class and usr MySQL table as shown below at liner number 6.
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.
<hibernate-mapping> contains all bean class mappings.
<class> create mapping in between class and table. name attribute value represents to class name and table attribute value represents to MySQL table name.
<property> represents to class declare class level variable. name attribute value represents to class level variable and type represents to data type of that variable.
<column> represents to column MySQL table. name attribute value represents to column name.
<id> represents to unique ID attribute in class map to the primary key of the database table.
<generator> this helps to generate unique id for given column.
<one-to-many> will help us to create a one-to-many mapping with other (Address) class.
one-to-many tag is having name attribute to declare class name to map with and other attribute to declare cascade type.
Important: <key> tag having column attribute which will save userId in address entity id field.
Address table id field will save userId from user table while saving data.
8. Create address.hbm.xml file to provide mapping of Address class and address_tables MySQL table as shown below at liner number 6.
Create mapping for class variables and table columns.
It will also require to create a mapping of Address.java class with User.java as highlighted below red in color.
9. Create hiernate.cfg.xml file to provide info related to database connection as mentioned below.
Load user.hbm.xml and address.hbm.xml file using mapping tag as mentioned at line number 25 and 26.
10. 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
While data insertion in address_t table, code will create a new column named as userId and type to make a relation in between user_t and address_s table as shown below.
11. Create a table user_t and address_s in MySQL using below query.
create table user_t(userId int NOT NULL AUTO_INCREMENT, firstName varchar(20), lastName varchar(20), age int, primary key(userid));
create table address_s(id int NOT NULL AUTO_INCREMENT, addressline varchar(20), city varchar(20), pincode int, primary key(id));
12 Run App.java main function as Java application will insert the data in the database as shown below.
OUTPUT:
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.