• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

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.

Core Idea

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.

How It Works

  1. @Entity: Marks a POJO class as a Hibernate entity mapped to a database table
  2. @Table(name): Specifies the database table name (optional; defaults to class name)
  3. @Id: Marks the primary key field
  4. `@GeneratedValue(strategy): Specifies primary key generation: AUTO, IDENTITY, SEQUENCE, TABLE
  5. `@Column(name, nullable, length): Maps a field to a column with optional constraints
  6. @Transient: Marks a field that should NOT be persisted
  7. `@Temporal(TemporalType.DATE): Specifies date/time precision for java.util.Date fields
  8. `@Enumerated(EnumType.STRING): Specifies enum storage as STRING (name) or ORDINAL (index)

Visual Explanation

hibernate_annotations POJO Plain Java Class (Entity) ANNO Annotations POJO->ANNO ENTITY @Entity ANNO->ENTITY TABLE_A @Table(name) ANNO->TABLE_A ID @Id ANNO->ID GEN @GeneratedValue ANNO->GEN COL @Column(name) ANNO->COL TABLE Database Table ROW1 ROW1 TABLE->ROW1 rows ROW2 ROW2 TABLE->ROW2 rows ENTITY->TABLE TABLE_A->TABLE COL->TABLE

Key Properties

  • JPA-standard: Annotations come from javax.persistence.* (or jakarta.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

Connections

Edge Cases & Gotchas

  • Field vs property access: @Id placement 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