@Id marks the persistent property or field that supplies an entity’s database identity. In JPA, every entity must have an identifier; without @Id, @Entity alone cannot produce a complete mapping. An identifier named id conventionally maps to an ID primary-key column when no further mapping is specified. section 5.2.2
The placement and surrounding annotations determine how the persistence framework handles the identifier:
- Place @Id on a field to select field access; Hibernate or Spring Data JPA reads and writes the identifier directly and treats fields as persistent by default. section 5.2.2
- Expose a getter when application code needs an identifier as a lookup handle, such as selecting an item from search results. section 5.2.2
Generated identifier on a JPA entity
@Entity public class Item { @Id @GeneratedValue(generator = "ID_GENERATOR") private Long id; public Long getId() { return id; } }
Explicit identifier mapping with Spring Data JDBC
@Table("USERS") public class User { @Id @Column("ID") private Long id; @Column("USERNAME") private String username; }
Inspect the complete mapped Item entity showing @Id, generated-value configuration, a Long identifier, and a public getter. section 5.2.2

