OneToMany in Hibernate using Annotations

OneToMany in Hibernate using Annotations
Here we are going to implement OneToMany mapping in hibernate using Annotations.

We are going to have 2 entities Address and Employee. Here we are assuming that an employee can have more than one address. When we enter the employee details and his addresses (Here we are going to enter 2 addresses for one employee) a 3rd table creating which brings the relationship between the both Employee and Address table.

op1

Once we go through the example, the concept will be clear.

Structure:
Structure

hibernate.cfg.xml

NewHibernateUtil.java

Address.java

Employee.java

Execution.java

Output:
op1

We can see 3 tables has been created, Employee_Mapping, Address_Mapping and Joined_Table. Here the relationship between the Employee_Mapping and Address_Mapping is pointed out in JoinedTable

op4

we can see that employee_ID – 3, that employee has 2 address that are mapped in address table (5,6)

This is know an OneToMany Mapping

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *