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).
import jakarta.persistence.*;

@Entity // Marks this class as a Hibernate entity
@Table(name = "students") // Maps to 'students' table in the database
public class Student {
// Constructors, Getters & Setters
}


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

Example: Auto-increment ID
import jakarta.persistence.*;
@Entity // Marks this class as a Hibernate entity
@Table(name = "students") // Maps to 'students' table in the database
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
Example: Using a Sequence Generator
import jakarta.persistence.*;
@Entity // Marks this class as a Hibernate entity
@Table(name = "students") // Maps to 'students' table in the database
public class Student {
@SequenceGenerator(name = "student_seq", sequenceName = "student_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_seq")
private Long id;
}

Column Mapping (@Column)

  • @Column → Defines column mapping (optional, default: field name = column name).
  • This maps email to the student_email column.
Example: Using @Column
import jakarta.persistence.*;
@Entity // Marks this class as a Hibernate entity
@Table(name = "students") // Maps to 'students' table in the database
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "student_email", unique = true, nullable = false)
private String email;
}

Attribute

Description

name

Custom column name

nullable

Allows NULL values (default: true)

length

Maximum column length (default: 255 for String)

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

Example: Mapping Different Data Types
@Column(name = "is_active", columnDefinition = "BOOLEAN DEFAULT TRUE")
private boolean active;

@Column(name = "created_at")
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;

✅ The createdAt field is mapped to a TIMESTAMP.


 Embedded Objects (@Embeddable, @Embedded)

  • Use Embeddable objects to group multiple fields in the same table.
Address.java
@Embeddable
public class Address {
private String city;
private String state;
}
Student.java
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Embedded
private Address address;
}

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.