Hibernate Mapping

Manish Kumar
3 min readJan 13, 2023

Mapping in Hibernate refers to the process of creating a relationship between the Java classes and the database tables. Hibernate uses various mapping strategies to map the Java classes to the database tables. These strategies include:

  1. XML Mapping: In this strategy, the mapping information is defined in an XML file, usually named hibernate.cfg.xml or hibernate.properties. This file contains the information needed to connect to the database and the mapping information for the Java classes.
  2. Annotation Mapping: In this strategy, the mapping information is defined using annotations in the Java classes. These annotations are part of the Java Persistence API (JPA) and are used to define the mapping information for the class and its properties.
  3. Programmatic Mapping: In this strategy, the mapping information is defined programmatically using the Hibernate Configuration and SessionFactory classes. This approach is less common and used when more control is needed over the mapping process.

The mapping process is responsible for creating the relationship between the Java classes and the database tables, so that Hibernate can perform the necessary operations, such as saving, retrieving, and updating the data.

For example, when you map a Java class to a database table, you need to define how the class properties map to the table columns and how the relationships between the classes map to the relationships between the tables.

Overall, Mapping in Hibernate is the process of creating a relationship between the Java classes and the database tables, it can be done in different ways such as XML, Annotation or programmatically, this mapping information allows Hibernate to perform the necessary operations on the data stored in the database.

Hibernate supports several types of mapping strategies to map the Java classes to the database tables. These include:

One-to-one mapping: This type of mapping is used to map a one-to-one relationship between two classes. For example, a one-to-one relationship between an “Employee” class and a “Passport” class, where one employee has one passport.

One-to-many mapping: This type of mapping is used to map a one-to-many relationship between two classes. For example, a one-to-many relationship between a “Department” class and an “Employee” class, where one department has many employees.

Many-to-one mapping: This type of mapping is used to map a many-to-one relationship between two classes. For example, a many-to-one relationship between an “Employee” class and a “Department” class, where many employees belong to one department.

Many-to-many mapping: This type of mapping is used to map a many-to-many relationship between two classes. For example, a many-to-many relationship between an “Employee” class and a “Project” class, where many employees can work on many projects.

Component mapping: This type of mapping is used to map a component (a class that contains simple properties) as a single value within another class.

Collection mapping: This type of mapping is used to map a collection of values, such as a list or set, within a class.

Inheritance mapping: This type of mapping is used to map a class hierarchy, where a parent class has one or more child classes.

Embeddable mapping: This type of mapping is used to map an embeddable class, which is a class that is used as a value type within another class, rather than being persisted as a separate entity.

It’s worth noting that Hibernate provides different ways to implement the same type of mapping, for example, mapping can be done using XML, annotation, or programmatically.

--

--