Step 6- Mapping in Hibernate

Mapping meaning in hibernate-

Connection between java class to database tables

Types of mapping in hibernate-

1- Inheritance Mapping-

When java classes are having inheritance, their tables can be created using 3 ways-

1- Table per class hierarchy –
-Only one table is created for all the classes.
-One Discriminator field is used.
xml element <subclass> is used.
-Gives maximum performance

Class View
Table view

2–Table per sub-class hierarchy-
-One table for each class is created.
-Foreign key is maintained between the tables.
-xml element <joined- subclass> is used.

Class view
Table view

3-Table per concrete class hierarchy-
-One table for each concrete class (subclass) is created but not of super classes.
-Foreign key is not maintained.
-xml element<union-subclass> is used.

Class view

2- Collection Mapping-

When java class is having collection object, they can be stored according to their type of collection –

1-list mapping
2-set mapping
3-map mapping

3- Association mapping-

When java class is having reference of another java class, their tables can be created using 3 ways-

1-One to one –
Real world example-
Student and Roll no. have one-to-one association, a student can have only 1 roll no. and a roll no. can have only 1 student.

Extra configuration-
 add <one-to-one> property into hbm xml files.
-assign key generation of one class as foreign key referring to other class.

2-One to many or many to one
Real world Example-
Student and notebooks have one to many relationship, as a student can have many notebooks.
Many notebooks can belongs to one student.

3-Many to many
Real World Example-
Students and Teachers have many to many relationship, as multiple students can have multiple teachers and multiple teachers can have multiple students.