Hibernate provides basic mappings to define how Java objects (entities) are mapped to relational database tables using annotations or XML configuration.
@Entity
→ Marks a class as a Hibernate entity.@Table
→ Specifies the table name (optional).@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
→ Defines column mapping (optional, default: field name = column name).email
to the student_email
column.
Attribute |
Description |
---|---|
name |
Custom column name |
nullable |
Allows |
length |
Maximum column length (default: 255 for |
unique |
Ensures column values are unique |
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
.