An entity needs a database primary key even when its Java object has no identifier yet. With `GenerationType.IDENTITY`, the database supplies a numeric key through an auto-incremented primary key column when the row is inserted. Select it on the entity’s `@Id` field with `@GeneratedValue(strategy = GenerationType.IDENTITY)`. section 5.2.4
Persisting an entity
An `Item` can declare `@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;`. After `entityManager.persist(item)`, do not assume `item.getId()` is already non-null: an ORM may defer the INSERT, while an identity-column value becomes available only after that INSERT. section 5.2.4 section 5.2.5
Choose the generation strategy according to when the application needs the key:
- `IDENTITY` uses an auto-incremented column and produces its value on INSERT. section 5.2.4
- `SEQUENCE` obtains a value separately before INSERT. An enhanced-sequence generator is recommended when portable, pre-insert identifiers and optimization options matter. section 5.2.4 section 5.2.5
- `TABLE` reads and updates an extra table before INSERT; `AUTO` delegates the choice to the configured database dialect. section 5.2.4
A database identifier is not the same as Java instance identity (`==`) or equality (`equals()`). Different Java objects can represent the same row, and a closed persistence context no longer guarantees identity mapping for detached instances. section 1.2.3 section 10.3
