liveBook · Manning

Primary key

As explained in Java Persistence with Spring Data and Hibernate epub

A primary key is a database column or set of columns whose values uniquely identify a row.

A primary key identifies a database row by its column value or combination of column values. For a persistent entity, the database identifier maps to the table’s primary key. This is database identity, not necessarily Java instance identity (`==`) or object equality (`equals()`): distinct Java objects can represent the same row. section 5.2.3 section 1.2.3

Choose a key that remains reliable throughout the row’s lifetime:

  • Require a non-null, unique value. A composite key can use several columns; a candidate key should not include columns unnecessary for uniqueness. section 5.2.3
  • Keep the primary-key value unchanged. Hibernate and Spring Data JPA with Hibernate as provider do not support updating it through their API; workarounds can disrupt caching and dirty checking. section 5.2.3
  • Consider a surrogate key when no suitable natural key exists. Keep another genuinely unique candidate key under a database unique constraint. section 1.2.3 section 5.2.3
CREATE TABLE USERS (
    ID BIGINT NOT NULL PRIMARY KEY,
    USERNAME VARCHAR(15) NOT NULL UNIQUE,
     . . . 
);
CREATE TABLE BILLINGDETAILS (
    ID BIGINT NOT NULL PRIMARY KEY,
    ACCOUNT VARCHAR(15) NOT NULL,
    BANKNAME VARCHAR(255) NOT NULL,
    USER_ID BIGINT NOT NULL,
    FOREIGN KEY (USER_ID) REFERENCES USERS(ID)
);

Compare the generated `USERS.ID` primary key with the still-unique `USERNAME`: `BILLINGDETAILS.USER_ID` references the stable identifier rather than a username that might change. section 1.2.3

The table structure also determines which values need independent keys. An embedded `Address` shares its owning `User` row and identifier; it has no separate identity or lifecycle. For a set of embedded images, the collection table instead uses a composite primary key: `ITEM_ID` plus the non-null embedded columns `FNAME`, `WIDTH`, and `HEIGHT`. Including `ITEM_ID` distinguishes otherwise equal images belonging to different items. section 6.2.1 section 8.2.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines candidate-key requirements and explains key selection, immutability, and provider consequences; generator strategies are noted but not detailed here.
Examples
Contrasts a username key with generated user IDs and shows a composite key for a set of embedded images.
What it assumes
Builds on Java identity and equality while introducing candidate keys and database identity.
What you can do after
Evaluate/understand which candidate key should identify an entity and why changing that key causes persistence problems.
How it connects
Distinguishes database identity from Java identity and object equality, and relates keys to foreign keys, embedded values, and collection tables.

Also explained in

Compare how 21 books explain primary key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.