Open In App

Hibernate - Types of Mapping

Last Updated : 25 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  1. Primitive Types
  2. Date and Time Types
  3. Binary and Large Object Types
  4. 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 TypeWrapper ClassANSI SQL Type
integerint or java.lang.IntegerINTEGER
characterjava.lang.StringCHAR(1)
floatfloat or java.lang.FloatFLOAT
stringjava.lang.StringVARCHAR
doubledouble or java.lang.DoubleDOUBLE
booleanboolean or java.lang.BooleanBIT
shortshort or java.lang.ShortSMALLINT
longlong or java.lang.LongBIGINT
bytebyte or java.lang.ByteTINYINT
big_decimaljava.math.BigDecimalNUMERIC

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 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:

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 typeWrapper ClassANSI SQL Type
clobjava.sql.ClobCLOB
blobjava.sql.BlobBLOB
binarybyte[]VARBINARY (or BLOB)
textjava.lang.StringCLOB
serializableany Java class that implements java.io.SerializableVARBINARY (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 typeWrapper ClassANSI SQL Type
classjava.lang.Class VARCHAR
localejava.util.LocaleVARCHAR
currencyjava.util.CurrencyVARCHAR
timezonejava.util.CurrencyVARCHAR

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;


Article Tags :

Explore