Hibernate Mapping
In this tutorial we will discuss Hibernate mapping.
Hibernate Mapping :
Hibernate mapping is of different types. database tables mapping ,O/R mapping
etc.
Here we are discussing Object/Relational mapping. In general, you can do O/R
mapping in XML file.
You can also do mapping by using annotations.
Types of mapping :There are four kinds of mapping you can done through
hibernate-
- One-to-One mapping
- One-to-Many mapping
- Many-to-One mapping
- Many-to-Many mapping
One-to-One Mapping : One-to-One relationship is
single-valued in both the directions. In one to one
relationships occurs when one entity is exactly related to single occurrence in
another entity.
You can say , each row in table A is linked to only one row of B .
Example :If we take two table as employee and department.
Then the relation one head of department manages one department and each
department has only one head.
you can see ,there is one to one relationship from both side.
One-to-Many Mapping : One-to-Many relationship is
single valued in one side and multi valued in other side. It means one entity is
related to many occurrence in another entity.
You can say, each record of one table A is linked to multiple records in another
table B.
-
Example :
- A car and its parts. Each part belongs to one car and one car has multiple parts.
- Employee and phone number, One employee can have many phone numbers but phone number belongs to only one employee.
Many-to-One Mapping : Many-to-one relationship is multi
valued in one side and single valued in other side. It means many occurrence of
a entity is related to one occurrence in another entity.
You can say, multiple records of one table A is linked to one record in another
table B.
Example : Employee and address. Many employee live in same house so many employee belongs to one address.
Many-to-Many Mapping : Many-to-Many relationship is
multi valued in both side. It means many occurrence of a entity is related to
many occurrence in another entity.
You can say, multiple rows of one table A is linked to multiple rows in
another table B.
Example : A student can have many subjects .In the same way one subject can have many students so here is many-to many relationship.