Advertisement
Google Ad Slot: content-top
Hibernate Mappings
Hibernate provides basic mappings to define how Java objects (entities) are mapped to relational database tables using annotations or XML configuration.
Entity Mapping (@Entity, @Table)
@Entity→ Marks a class as a Hibernate entity.@Table→ Specifies the table name (optional).
Primary Key Mapping (@Id, @GeneratedValue)
@Id→ Marks a field as a primary key.@GeneratedValue→ Specifies primary key generation strategy.
Strategy |
Description |
|---|---|
IDENTITY |
Auto-increment (MySQL, PostgreSQL) |
SEQUENCE |
Uses a database sequence (Oracle, PostgreSQL) |
TABLE |
Stores generated IDs in a table |
AUTO |
Chooses strategy based on DB |
Column Mapping (@Column)
@Column→ Defines column mapping (optional, default: field name = column name).- This maps
emailto thestudent_emailcolumn.
Attribute |
Description |
|---|---|
name |
Custom column name |
nullable |
Allows |
length |
Maximum column length (default: 255 for |
unique |
Ensures column values are unique |
Default Data Types Mapping
Hibernate automatically maps Java types to SQL types.
Java Type |
SQL Type |
|---|---|
String |
VARCHAR |
int,Integer |
INTEGER |
long,Long |
BIGINT |
double,Double |
DOUBLE |
boolean,Boolean |
BOOLEAN |
Date,LocalDateTime |
TIMESTAMP |
✅ The createdAt field is mapped to a TIMESTAMP.
Embedded Objects (@Embeddable, @Embedded)
- Use Embeddable objects to group multiple fields in the same table.