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.
@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 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: This code shows how an Employee entity is mapped to a database table with date and time fields using Hibernate.
@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 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 | Serializable | VARBINARY (or BLOB) |
Example: This code demonstrates how to store large binary and text data in a database using Hibernate.
@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 type | Wrapper Class | ANSI SQL Type |
|---|---|---|
| class | java.lang.Class | VARCHAR |
| locale | java.util.Locale | VARCHAR |
| currency | java.util.Currency | VARCHAR |
| timezone | java.util.TimeZone | VARCHAR |
Example: This code shows how to map a Java enum to a database column using Hibernate.
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