@GeneratedValue tells JPA that the value of an entity's @Id field is generated by the persistence provider or the database, not assigned by your code. You choose how with the strategy attribute: AUTO, IDENTITY, SEQUENCE, TABLE, or, in Jakarta Persistence 3.2 and later, UUID. The id becomes available after the entity is persisted, and with some strategies it is only available after the insert runs.
Picking the right strategy affects portability, performance (especially JDBC batching) and how ids behave under concurrency.
Without @GeneratedValue, you must set the primary key yourself before calling persist. With it, the provider (usually Hibernate) obtains a unique value for you:
Spring Boot 3 and later use the jakarta.persistence package. Older code uses javax.persistence, but the annotations behave the same way.
The provider chooses a strategy for the database. This is the default when you write @GeneratedValue with no arguments. In Hibernate 6 and later, a Long id with AUTO uses a sequence (named after the entity, such as employee_seq) on databases that support sequences, and an emulated sequence table otherwise. Because the result varies by provider and version, many teams state the strategy explicitly.
The database generates the value through an auto-increment or identity column (MySQL AUTO_INCREMENT, PostgreSQL GENERATED ... AS IDENTITY, SQL Server IDENTITY). Hibernate must run the insert immediately to learn the id, so it cannot defer inserts, and JDBC batch inserts are disabled for such entities.
The id comes from a database sequence, fetched before the insert. This allows batching and is usually the best choice on PostgreSQL, Oracle and other databases with sequences.
allocationSize (default 50) means Hibernate reserves 50 ids per database round trip. The sequence's INCREMENT BY must match it, otherwise you get duplicate or skipped ids.
The provider keeps the next value in a separate table. It works everywhere but is the slowest and causes lock contention, so avoid it unless the database has neither sequences nor identity columns.
Added in Jakarta Persistence 3.2, it generates a UUID id with a supported type such as java.util.UUID:
On older versions, use Hibernate's @UuidGenerator for the same effect.
| Strategy | Who generates | Batch inserts | Best for |
|---|---|---|---|
IDENTITY | Database column | No | MySQL, SQL Server, simple apps |
SEQUENCE | Database sequence | Yes | PostgreSQL, Oracle, high write volume |
UUID | Provider | Yes | Distributed ids, ids exposed in URLs |
TABLE | Table row | Yes, but slow | Legacy portability only |
AUTO | Provider decides | Depends | Prototypes, quick starts |
If you pair sequences with Flyway or Liquibase, create the sequence in the migration with an increment equal to allocationSize, and set ddl-auto to validate.
null until persisted. With IDENTITY it is set at persist time (the insert runs then). With SEQUENCE it is also set at persist, but the insert may run later at flush.@GeneratedValue on composite keys or on fields you assign yourself. Use @EmbeddedId or assign the value.Long, not long, so a transient entity can be recognised by a null id.GenerationType.AUTO. The provider selects the mechanism, and in Hibernate 6 and later that is normally a sequence for a numeric id.
You are likely using IDENTITY. Switch to SEQUENCE and enable hibernate.jdbc.batch_size.
Yes with UUID (a String mapped UUID is supported by modern Hibernate) or a custom generator. Numeric strategies need a numeric type.
No. Without it Hibernate uses a default sequence name. Add it when you need a specific name or allocation size.
@GeneratedValue makes JPA responsible for primary key creation. Use SEQUENCE for performance and batching where sequences exist, IDENTITY for simple auto-increment columns, UUID for distributed ids, and be explicit instead of relying on AUTO. Keep sequence increments aligned with allocationSize and manage the schema through migrations.