Primary Keys and @GeneratedValue Strategies
Learn Primary Keys and @GeneratedValue Strategies (Phase 2 · Hibernate Core Concepts (Essential), Hibernate) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Choosing an identifier • @Id marks the primary key. Types: Long (most common), Integer , UUID , String (natural keys), embedded/composite keys. Use wrapper types ( Long , not long ) so a transient entity has id == null . • @GeneratedValue(strategy) lets the database or Hibernate generate it: IDENTITY (auto-increment column), SEQUENCE (database sequence, with a generator), TABLE (rarely), AUTO (provider picks: sequence on PostgreSQL/Oracle, identity or sequence emulation on MySQL), and UUID (Hibernate 6: GenerationType.UUID ).