Early Hibernate used XML mapping files (.hbm.xml) to define how Java classes mapped to database tables. These files were verbose, hard to maintain, and separated the mapping definition from the code it described, making refactoring error-prone.
Hibernate annotations allow developers to specify entity mappings directly in Java code using JPA-standard annotations. The most common annotations replace XML configuration with in-code metadata: @Entity, @Table, @Id, @Column, @GeneratedValue.
@Entity: Marks a POJO class as a Hibernate entity mapped to a database table@Table(name): Specifies the database table name (optional; defaults to class name)@Id: Marks the primary key field- `@GeneratedValue(strategy): Specifies primary key generation: AUTO, IDENTITY, SEQUENCE, TABLE
- `@Column(name, nullable, length): Maps a field to a column with optional constraints
@Transient: Marks a field that should NOT be persisted- `@Temporal(TemporalType.DATE): Specifies date/time precision for java.util.Date fields
- `@Enumerated(EnumType.STRING): Specifies enum storage as STRING (name) or ORDINAL (index)
- JPA-standard: Annotations come from
javax.persistence.*(orjakarta.persistence.*), not Hibernate-specific - Zero XML: Full mapping can be done with annotations alone — no .hbm.xml files needed
- Compile-time checked: Wrong annotation usage is caught at compile time vs XML’s runtime failures
- Default conventions: Unspecified mappings default to sensible conventions (table = class name, column = field name)
- Hybrid possible: Annotations can override or supplement XML configurations
- Built from: Hibernate ORM Framework — Annotations are the modern way to configure Hibernate
- Built from: Object-Relational Mapping — Annotations map Java objects to relational tables
- Related: Hibernate Entity Mapping — Relationship annotations (@OneToOne, @OneToMany, @ManyToMany)
- Contrasts with: EJB Deployment Descriptor — XML-based vs annotation-based configuration
- Builds into: Spring Data JPA — Spring Data JPA entities use the same JPA annotations
- Field vs property access:
@Idplacement determines access strategy — on field (FIELD access) or getter (PROPERTY access); mixing causes issues - Default column names: Auto-generated column names follow naming strategy; explicit
@Column(name)avoids surprises - GenerationType.IDENTITY: Disables batch inserts because the DB must generate the ID before Hibernate knows it
- @Enumerated(ORDINAL): Default is ORDINAL (numeric), which breaks if enum ordering changes — prefer STRING