liveBook · Manning

Unique identifier

As explained in Java Persistence with Spring Data and Hibernate epub

An entity property whose value identifies its corresponding persistent database record.

A unique identifier is an entity property whose value exposes database identity to the application. Entity classes generally require one, while value types are identified through their owning entity. section 5.1.3 section 5.2.2

Use the identifier property consistently across mapped entities:

  • Annotate the property with @Id; without additional mapping, it maps to a primary-key column named ID. section 5.2.2
  • Prefer a shared property name such as id across domain entities, supporting predictable lookup and mapping conventions. section 5.2.2
  • Place @Id on a field when field access is preferred; the persistence provider then accesses fields directly and treats fields as persistent by default. section 5.2.2
  • Expose a getter when application code needs the identifier as a handle for retrieving a selected entity. section 5.2.2
Listing 5.1 Mapped Item entity class with an identifier property
Path: Ch05/generator/src/main/java/com/manning/javapersistence/ch05/model
 /Item.java
 
@Entity
\1 Item {
    @Id
    @GeneratedValue(generator = "ID_GENERATOR")
    private Long id;
    public Long getId() {
        return id;
    }
}

Shows a mapped Item entity with an @Id, generated Long identifier, and getter. section 5.2.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines identifier properties through entity rules, default column mapping, field access, and an annotation-based Java example.
Examples
Uses a mapped Item entity with a generated Long id and getter, plus search-result retrieval by identifier.
What it assumes
Assumes persistence-capable Java classes and distinguishes entity identity from value-type ownership.
What you can do after
Operate/configure mapped entities with @Id, predictable identifier names, field access, and optional application-facing getters.
How it connects
Connects entities, value types, primary keys, field access, JPA, Hibernate, Spring Data JPA, and transparent persistence.

Also explained in

Compare how 9 books explain unique identifier →

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.