Topic 7- Types of Mapping in Hibernate

image 2

Play Store Application link – Hibernate – in 10 steps – Apps on Google Play

Mapping meaning in hibernate-

Connection between java class to database tables

Types of mapping in hibernate-

There are 3 types of mapping in hibernate for classes.

1- Inheritance Mappings-
A-
Table per class hierarchy (Single Table Strategy) –
B- Table per sub-class hierarchy (Joined Strategy)-
C- Table per concrete class hierarchy-

2- Collections Mappings-
A-
List Mapping
B- Set Mapping
C- Map Mapping

3- Association Mappings-
A-
One to One Mapping
B- One to Many Mapping
C- Many to One Mapping
D- Many to Many Mapping

1- Inheritance Mapping-

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

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

Github Project to import (Xml Version) – https://github.com/kuldeep101990/SingleTableStrategy

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/SingleTableStrategyAnnotation


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

Github Project to import (Xml Version) – https://github.com/kuldeep101990/JoinedTableStrategy

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/JoinedTableStrategyAnnotation



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.

Github Project to import (Xml Version) – https://github.com/kuldeep101990/TablePerConcreteClassStrategy

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/TablePerConcreteClassStrategyAnnotation

2- Collection Mapping-

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

collectiondescriptionxml based mappingannotation based mapping
ListA list of entities mapped to a single table.<list name=”employees” table=”employees_list”><key column=”employee_id”/><index column=”employee_index”/><one-to-many class=”Employee”/></list>@ElementCollection
@CollectionTable(name=”employees_list”, joinColumns=@JoinColumn(name=”employee_id”))
@Column(name=”employee_index”)
private List<Employee> employees;
SetA set of entities mapped to a single table.<set name=”employees” table=”employees_set”><key column=”employee_id”/><one-to-many class=”Employee”/></set>@ElementCollection
@CollectionTable(name=”employees_set”, joinColumns=@JoinColumn(name=”employee_id”))
private Set<Employee> employees;
MapA map of entities mapped to a single table, where the keys and values are both entities.<map name=”employees” table=”employees_map”><key column=”employee_id”/><map-key column=”employee_key”/><one-to-many class=”Employee”/>
</map>
@ElementCollection
@CollectionTable(name=”employees_map”, joinColumns=@JoinColumn(name=”employee_id”))
@MapKeyColumn(name=”employee_key”)
private Map<String, Employee> employees;

1-List Mapping

Github Project to import (Xml Version) – https://github.com/kuldeep101990/ListCollectionMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/ListCollectionMappingAnnotation


2-Set Mapping

Github Project to import (Xml Version) – https://github.com/kuldeep101990/SetCollectionMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/SetCollectionMappingAnnotation


3-Map Mapping

Github Project to import (Xml Version) – https://github.com/kuldeep101990/MapCollectionMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/MapCollectionMappingAnnotation

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 –

Github Project to import (Xml Version) – https://github.com/kuldeep101990/OneToOneMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/OneToOneMappingAnnotation

2-Many to One

Github Project to import (Xml Version) – https://github.com/kuldeep101990/ManyToOneMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/ManyToOneMappingAnnotation

3- One to Many-

Github Project to import (Xml Version) – https://github.com/kuldeep101990/OneToManyMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/OneToManyMappingAnnotation

4-Many to many

Github Project to import (Xml Version) – https://github.com/kuldeep101990/ManyToManyMapping

Github Project to import (Annotation Version) –

https://github.com/kuldeep101990/ManyToManyMappingAnnotation