What is @GeneratedValue Annotation in JPA?
@GeneratedValue is a JPA annotation used to automatically generate primary key values for entity objects.
It works together with:
@Id
annotation to generate unique IDs automatically.
In simple words:
“@GeneratedValue automatically creates unique primary key values for database records.”
Why @GeneratedValue is Needed
Without automatic ID generation:
- Developers must manually assign IDs
- Duplicate IDs may occur
- Primary key conflicts may happen
- Database maintenance becomes difficult
@GeneratedValue solves these problems by generating unique IDs automatically.
Basic Example
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}
What Happens Internally?
When a new student is saved:
- Developer does not provide ID
- Hibernate generates unique ID automatically
- Database stores generated ID
Example Save Operation
Student student = new Student();
student.setName("Naresh");
student.setEmail("naresh@gmail.com");
repository.save(student);
Generated Database Record
| ID | Name | |
|---|---|---|
| 1 | Naresh | naresh@gmail.com |
Why @GeneratedValue Works with @Id
@Id identifies the primary key field,
while:
@GeneratedValue
generates the primary key value automatically.
Common Syntax
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
private Long id;
Main Generation Strategies
JPA supports multiple ID generation strategies.
GenerationType Strategies
| Strategy | Description |
|---|---|
| IDENTITY | Uses database auto-increment |
| SEQUENCE | Uses database sequence |
| TABLE | Uses separate generator table |
| AUTO | Hibernate chooses automatically |
1. IDENTITY Strategy
IDENTITY uses database auto-increment columns.
IDENTITY Example
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
Generated MySQL Table
CREATE TABLE student (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
)
When to Use IDENTITY
Commonly used with:
- MySQL
- SQL Server
Advantages of IDENTITY
- Simple configuration
- Database handles ID generation
- Widely used
Disadvantages of IDENTITY
- Batch inserts may be slower
- Database-dependent
2. SEQUENCE Strategy
SEQUENCE uses database sequence objects.
SEQUENCE Example
@GeneratedValue(
strategy = GenerationType.SEQUENCE
)
Sequence Generator Example
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "student_seq"
)
@SequenceGenerator(
name = "student_seq",
sequenceName = "student_sequence",
allocationSize = 1
)
private Long id;
When to Use SEQUENCE
Commonly used with:
- Oracle
- PostgreSQL
Advantages of SEQUENCE
- Better performance
- Efficient batch inserts
- More scalable
3. TABLE Strategy
TABLE strategy stores ID values in a separate table.
TABLE Example
@GeneratedValue(
strategy = GenerationType.TABLE
)
How TABLE Strategy Works
Hibernate creates:
id_generator
table to track ID values.
Disadvantages of TABLE Strategy
- Slower performance
- Additional table overhead
4. AUTO Strategy
AUTO lets Hibernate choose the best strategy automatically.
AUTO Example
@GeneratedValue(
strategy = GenerationType.AUTO
)
How AUTO Works
Hibernate selects strategy based on:
- Database type
- Hibernate dialect
Difference Between Strategies
| Strategy | Performance | Database Dependency |
|---|---|---|
| IDENTITY | Moderate | High |
| SEQUENCE | High | Moderate |
| TABLE | Lower | Low |
| AUTO | Depends on provider | Moderate |
How Hibernate Uses @GeneratedValue
- Entity object is created
- Hibernate checks ID strategy
- Unique ID is generated
- Entity is inserted into database
Generated SQL Example
INSERT INTO student(name, email)
VALUES (?, ?)
Database automatically generates:
id
Best Practices for @GeneratedValue
- Use IDENTITY for MySQL
- Use SEQUENCE for PostgreSQL and Oracle
- Prefer Long or UUID for IDs
- Avoid manual ID assignment when unnecessary
- Keep primary keys immutable
UUID-Based ID Example
Modern applications often use UUIDs.
UUID Example
@Id
@GeneratedValue
private UUID id;
Advantages of @GeneratedValue
- Automatic ID generation
- Prevents duplicate IDs
- Improves scalability
- Reduces manual coding
- Simplifies database operations
Disadvantages of Improper Strategy Selection
- Poor performance
- Database compatibility issues
- Slower inserts
- Complex migrations
Real-Time Example in Banking Application
Banking application contains entities such as:
- Customer
- Account
- Transaction
Each record requires a unique ID, which is generated automatically using:
@GeneratedValue
Difference Between @Id and @GeneratedValue
| Feature | @Id | @GeneratedValue |
|---|---|---|
| Purpose | Marks primary key | Generates primary key value |
| Mandatory | Yes | No |
| Used For | Entity identification | Automatic ID generation |
Common Interview Questions on @GeneratedValue
What is @GeneratedValue annotation?
It automatically generates primary key values for entities.
What are GenerationType strategies?
IDENTITY, SEQUENCE, TABLE, and AUTO.
Which strategy is commonly used in MySQL?
GenerationType.IDENTITY.
Which strategy is better for PostgreSQL?
GenerationType.SEQUENCE.
Can @GeneratedValue work without @Id?
No, it must be used together with @Id.
Conclusion
The @GeneratedValue annotation is an important JPA feature
used for automatic primary key generation.
It helps developers:
- Avoid manual ID management
- Prevent duplicate primary keys
- Improve scalability
- Simplify database operations
Understanding @GeneratedValue is essential for Spring Boot developers
because automatic ID generation
is widely used in enterprise applications,
REST APIs,
banking systems,
and microservices.