JPA ID Generation Strategies (@GeneratedValue)
In JPA, the @GeneratedValue annotation is used alongside @Id to automatically generate primary key values for entities. Choosing the right ID generation strategy is crucial for database consistency, performance, and scalability in Spring Data JPA and Hibernate applications.
1. GenerationType.AUTO
JPA provider automatically chooses the most suitable strategy based on the database (could be IDENTITY, SEQUENCE, or TABLE).
Usage:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
Key Points:
- Default strategy if none is specified.
- Database-independent; works across most databases.
- Good for simple use cases where strict control over ID generation is not required.
Use Case:
Portable applications where you don’t want to worry about database-specific ID generation.
2. GenerationType.IDENTITY
Uses the database’s auto-increment feature to generate IDs.
Usage:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Key Points:
- Supported by most relational databases (MySQL, PostgreSQL, SQL Server).
- The database generates the primary key automatically on insert.
- Simple and widely used.
- Limitation: Not ideal for batch inserts as the key is generated after insertion.
Use Case:
Applications using MySQL or other databases with auto-increment columns.
3. GenerationType.SEQUENCE
Uses a database sequence object to generate unique IDs.
Usage:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
Key Points:
- Commonly used in Oracle and PostgreSQL.
- Supports batching efficiently in Hibernate.
- Allows pre-allocation of ID blocks to improve performance.
- Can customize sequence name using
@SequenceGenerator.
Example with Custom Sequence:
@Id
@SequenceGenerator(name="user_seq", sequenceName="user_sequence", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="user_seq")
private Long id;
Use Case:
Applications needing high-performance inserts with sequence-supporting databases.
4. GenerationType.TABLE
Uses a separate table to maintain and generate unique IDs.
Usage:
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
Key Points:
- Database-agnostic strategy; works with any relational database.
- Maintains a special table to store the last generated ID.
- Less efficient than
IDENTITYorSEQUENCEfor high-volume applications. - Useful for databases without sequence or auto-increment support.
Use Case:
Legacy databases or situations where sequence/identity is not available.
Comparison Table
| Strategy | How It Works | Database Support | Pros | Cons |
|---|---|---|---|---|
| AUTO | JPA automatically selects a strategy based on the database | All databases | Easy to use, portable across databases | Behavior may vary depending on the DB |
| IDENTITY | Uses the database’s auto-increment feature | MySQL, SQL Server | Simple, widely supported | Inefficient for batch inserts |
| SEQUENCE | Uses a database sequence object to generate IDs | Oracle, PostgreSQL | Efficient, supports batch inserts | Requires database sequence support |
| TABLE | Maintains a separate table to generate IDs | Any database | Works on any DB, database-independent | Slower performance due to extra table lookup |
Best Practices
- AUTO: Use for simple applications needing portability.
- IDENTITY: Use with MySQL/PostgreSQL for auto-increment columns.
- SEQUENCE: Use for sequence-supported databases to optimize performance and batching.
- TABLE: Use only when the database has no native sequence or identity support.
Conclusion
JPA ID generation strategies allow automatic handling of primary keys in a flexible and database-aware manner. Choosing the right strategy ensures efficient, scalable, and maintainable Spring Data JPA applications.
AUTO– Simple, portable.IDENTITY– Easy, auto-increment.SEQUENCE– Efficient for sequence-based databases.TABLE– Database-independent, less efficient.
Using these strategies properly eliminates manual key management and simplifies ORM-based development with Hibernate and Spring Data JPA.
