Hibernate - Types of Mapping

Last Updated : 16 Jan, 2026

Hibernate is an open-source, lightweight ORM (Object Relational Mapping) framework that simplifies database interactions in Java applications. It implements the JPA (Java Persistence API) specification and allows developers to map Java classes to database tables and class fields to table columns. One of Hibernate’s core features is mapping, which defines how Java objects are persisted in relational databases.

Hibernate mappings are broadly classified into two categories:

1. Value / Basic Types Mapping

2. Association / Relationship Mapping

Value/Basic Types Mapping

Basic type mapping is used to map Java data types directly to database column types.

1. Primitive Types Mapping

Hibernate provides built-in support for mapping Java primitive and wrapper types to SQL types.

Java Type

Wrapper Class

ANSI SQL Type

int

Integer

INTEGER

char / String

String

CHAR(1)

float

Float

FLOAT

String

String

VARCHAR

double

Double

DOUBLE

boolean

Boolean

BIT

short

Short

SMALLINT

long

Long

BIGINT

byte

Byte

TINYINT

BigDecimal

BigDecimal

NUMERIC

Example: This code maps the Student Java class to a students database table using Hibernate annotations.

Java
@Entity
@Table(name = "students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;   
    private int age;       
    private boolean active; 

    // Getters & Setters
}

Explanation:

  • @Entity: Makes the class a Hibernate entity.
  • @Table(name = "students"): Maps the class to the students table.
  • @Id: Marks id as the primary key.
  • @GeneratedValue: Auto-generates the primary key value.
  • name, age, active: Map to VARCHAR, INTEGER, and BOOLEAN columns.
  • Getters & Setters: Allow Hibernate to access and manage field values.

2. Date and Time

Hibernate supports Java date and time classes for handling temporal data.

Mapping typeJava typeANSI SQL Type
datejava.util.Date or java.sql.DateDATE
timejava.util.Date or java.sql.TimeTIME
calendarjava.util.CalendarTIMESTAMP
timestampjava.util.Date or java.sql.TimestampTIMESTAMP
calendar_datejava.util.CalendarDATE

Example: This code shows how an Employee entity is mapped to a database table with date and time fields using Hibernate.

Java
@Entity
@Table(name = "employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    @Temporal(TemporalType.DATE)   
    private Date joiningDate;

    private LocalDateTime lastUpdated;  
}

Explanation:

  • name: Mapped to a VARCHAR column.
  • @Temporal(TemporalType.DATE): Stores only the date part of joiningDate.
  • LocalDateTime lastUpdated: Automatically mapped to a TIMESTAMP column (Java 8+ date/time support).

3. Binary and large objects 

Used to store large data such as images, documents, or videos.

Java typeWrapper ClassANSI SQL Type
clobjava.sql.ClobCLOB
blobjava.sql.BlobBLOB
binarybyte[]VARBINARY (or BLOB)
textjava.lang.StringCLOB
serializableSerializableVARBINARY (or BLOB)

Example: This code demonstrates how to store large binary and text data in a database using Hibernate.

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
}

Explanation:

  • fileName: Mapped to a VARCHAR column.
  • @Lob byte[] fileData: Stores binary data (like files/images) as a BLOB.
  • @Lob String description: Stores large text content as a CLOB.

4. JDK linked

Hibernate also supports mappings for certain Java utility classes.

Java typeWrapper ClassANSI SQL Type
classjava.lang.Class VARCHAR
localejava.util.LocaleVARCHAR
currencyjava.util.CurrencyVARCHAR
timezone

java.util.TimeZone

VARCHAR

Example: This code shows how to map a Java enum to a database column using Hibernate.

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
}

Explanation:

  • enum Role: Defines a fixed set of constants (ADMIN, USER, GUEST).
  • @Entity: Marks User as a Hibernate entity.
  • @Table(name = "users"): Maps the class to the users table.
  • @Id: Declares id as the primary key.
  • @GeneratedValue: Automatically generates the primary key.
  • username: Stored as a VARCHAR column.
  • @Enumerated(EnumType.STRING): Stores the enum value as a readable VARCHAR instead of an ordinal number.
  • role: Persists the user’s role safely and clearly in the database.

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 instance of an entity is associated with exactly one instance of another entity

@OneToOne
@JoinColumn(name = "profile_id")
private Profile profile;

Use Case: User ↔ Profile

2. One-to-Many

One entity is associated with multiple instances of another entity.

@OneToMany(mappedBy = "department")
private List<Employee> employees;

Use Case: Department → Employees

3. Many-to-One

Multiple entities are associated with a single entity.

@ManyToOne @JoinColumn(name = "department_id")
private Department department;

Use Case: Employees → Department

4. Many-to-Many Mapping

Multiple entities are associated with multiple entities using a join table.

@ManyToMany
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> courses;

Use Case: Students ↔ Courses

Comment