Hibernate - Types of Mapping
Last Updated :
25 Aug, 2025
Hibernate is an open-source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence. One of the most important features of Hibernate is its ability to handle mappings, which show how Java classes and their properties relate to database tables and columns.
Mapping in Hibernate can be categorized into two broad areas:
1. Value/Basic Types Mapping
- Primitive Types
- Date and Time Types
- Binary and Large Object Types
- JDK-related Types
2. Association/Relationship Mapping
- One-to-One
- One-to-Many
- Many-to-One,
- Many-to-Many.
Value/Basic Types Mapping
1. Primitive Types
These types of mapping have data types defined as “integer”, “character”, “float”, “string”, “double”, “Boolean”, “short”, “long”, etc. These are present in the Hibernate framework to map Java data types to RDBMS data types.
| Java Type | Wrapper Class | ANSI SQL Type |
|---|
| integer | int or java.lang.Integer | INTEGER |
| character | java.lang.String | CHAR(1) |
| float | float or java.lang.Float | FLOAT |
| string | java.lang.String | VARCHAR |
| double | double or java.lang.Double | DOUBLE |
| boolean | boolean or java.lang.Boolean | BIT |
| short | short or java.lang.Short | SMALLINT |
| long | long or java.lang.Long | BIGINT |
| byte | byte or java.lang.Byte | TINYINT |
| big_decimal | java.math.BigDecimal | NUMERIC |
Example:
Java
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name; // VARCHAR
private int age; // INTEGER
private boolean active; // BOOLEAN
// Getters & Setters
}
2. Date and Time
These are “date”, “time”, “calendar”, “timestamp” etc. Like primitive we have these date and time datatype mappings.
| Mapping type | Java type | ANSI SQL Type |
|---|
| date | java.util.Date or java.sql.Date | DATE |
| time | java.util.Date or java.sql.Time | TIME |
| calendar | java.util.Calendar | TIMESTAMP |
| timestamp | java.util.Date or java.sql.Timestamp | TIMESTAMP |
| calendar_date | java.util.Calendar | DATE |
Example:
Java
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@Temporal(TemporalType.DATE) // maps to SQL DATE
private Date joiningDate;
private LocalDateTime lastUpdated; // SQL TIMESTAMP
// Getters & Setters
}
3. Binary and large objects
These types are “clob”, “blob”, “binary”, “text” etc. Clob and blob data types are present to maintain the data type mapping of large objects like images and videos.
| Java type | Wrapper Class | ANSI SQL Type |
|---|
| clob | java.sql.Clob | CLOB |
| blob | java.sql.Blob | BLOB |
| binary | byte[] | VARBINARY (or BLOB) |
| text | java.lang.String | CLOB |
| serializable | any Java class that implements java.io.Serializable | VARBINARY (or BLOB) |
Example:
Java
@Entity
@Table(name = "files")
public class FileData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String fileName;
@Lob
private byte[] fileData; // Stored as BLOB
@Lob
private String description; // Stored as CLOB
}
4. JDK linked
Some of the mappings for objects which lie beyond the reach of the previous type of mappings are included in this category. These are “class”, “locale”, “currency”, “timezone”.
| Java type | Wrapper Class | ANSI SQL Type |
|---|
| class | java.lang.Class | VARCHAR |
| locale | java.util.Locale | VARCHAR |
| currency | java.util.Currency | VARCHAR |
| timezone | java.util.Currency | VARCHAR |
Example:
Java
public enum Role {
ADMIN, USER, GUEST
}
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
@Enumerated(EnumType.STRING)
private Role role; // Stored as VARCHAR
}
Association / Relationship Mapping
In real-world applications, entities often relate to one another. Hibernate provides annotations to map these relationships.
1. One-to-One
Each entity instance is linked to exactly one instance of another entity.
@OneToOne
@JoinColumn(name = "profile_id")
private Profile profile;
2. One-to-Many
One entity is related to multiple instances of another entity.
@OneToMany(mappedBy = "department")
private List<Employee> employees;
3. Many-to-One
Multiple instances of an entity relate to a single instance of another entity.
@ManyToOne @JoinColumn(name = "department_id")
private Department department;
4. Many-to-Many Mapping
Multiple entities relate to multiple entities. A join table is used to maintain the relationship.
@ManyToMany
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> courses;
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java