Hibernate / Hibernate 7 Basics Interview Questions
How does Hibernate 7 handle primary key generation strategies?
Primary key generation is controlled by @GeneratedValue. Hibernate 7 supports all four JPA strategies plus UUID as a first-class feature.
| Strategy | How it works | Best for |
|---|---|---|
| IDENTITY | DB auto-increment (SERIAL/AUTO_INCREMENT) | Simple setups; disables JDBC batch inserts |
| SEQUENCE | Uses DB sequence; Hibernate batches allocation | PostgreSQL, Oracle; supports batch inserts |
| TABLE | Simulates sequence with a special table | Portable but slow |
| AUTO | Hibernate picks best strategy | Quick prototyping |
| UUID | Generates UUID as PK | Distributed systems |
// IDENTITY: @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // SEQUENCE (recommended for batch inserts): @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "product_seq") @SequenceGenerator(name="product_seq", sequenceName="product_id_seq", allocationSize=50) private Long id; // UUID (first-class in Hibernate 7): @Id @GeneratedValue(strategy = GenerationType.UUID) private UUID id; // Time-based UUID v7: @Id @UuidGenerator(style = UuidGenerator.Style.TIME) private UUID id;
More Related questions...