Loading published curriculum…

14 · Hibernate and Spring Data JPA in depth / LESSON 65 OF 102

Identifiers, enums, embeddables, and converters

Mapping choices become long-lived database contracts.

Before this lesson: Dirty checking, flush, and commit

Understand the topic

Choose a generated identifier strategy compatible with your database and insert workload. Map enums with STRING when stored names are the intended contract; ordinal mappings are vulnerable to reordering. Renaming stored enum values still requires a migration.

Use @Embeddable for a value object stored as part of an entity and @AttributeOverride when column names differ. An AttributeConverter can translate a domain value to a basic column representation; do not hide query-relevant structure inside opaque strings without considering indexing.

EXAMPLE · JAVA

Address.java

import jakarta.persistence.*;
@Embeddable
public class Address {
 @Column(name = "city", nullable = false)
 private String city;
 protected Address() {}
 public Address(String city) {
  if (city == null || city.isBlank()) throw new IllegalArgumentException("city required");
  this.city = city;
 }
 public String city() { return city; }
}

What to expect

Embed this value in an entity with @Embedded and add its column through a migration. It has no independent entity identifier.

YOUR TURN

Put it into practice

  1. 01Add an embedded address and a string-mapped status enum.
  2. 02Inspect the schema and plan how an enum rename would migrate existing rows.
  3. 03Verify: A round-trip preserves values and the migration makes persisted enum meanings explicit.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Mapping choices become long-lived database contracts.

  1. 01Choose a generated identifier strategy compatible with your database and insert workload. Map enums with STRING when stored names are the intended contract; ordinal mappings are vulnerable to reordering. Renaming stored enum values still requires a migration.
  2. 02Use @Embeddable for a value object stored as part of an entity and @AttributeOverride when column names differ. An AttributeConverter can translate a domain value to a basic column representation; do not hide query-relevant structure inside opaque strings without considering indexing.
  3. 03Trace the example: Embed this value in an entity with @Embedded and add its column through a migration. It has no independent entity identifier.

A mistake worth avoiding

Reordering enum constants changes the meaning of stored numbers.

DEBUGGING ROOM

Something broke. Where do you look?

Reordering enum constants changes the meaning of stored numbers.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does an embeddable normally have independent entity identity?

Choose one answer

BUILD IT YOURSELF

Add an embedded address and a string-mapped status enum.

  1. 1.Add an embedded address and a string-mapped status enum.
  2. 2.Inspect the schema and plan how an enum rename would migrate existing rows.
  3. 3.Reproduce this failure, then repair it: Reordering enum constants changes the meaning of stored numbers.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does an embeddable normally have independent entity identity?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

A round-trip preserves values and the migration makes persisted enum meanings explicit.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references