@GeneratedValue is a JPA annotation used to let the persistence provider automatically assign values to primary key fields. It eliminates manual ID management and ensures each entity gets a unique identifier when stored in the database.
- Works with @Id to define and auto-generate primary keys
- Delegates ID generation responsibility to the database or provider
- Improves consistency and avoids errors caused by duplicate or missing IDs
Generation Strategies of @GeneratedValue Annotation
The @GeneratedValue annotation provides us with different strategies for the generation of primary keys, which are as follows :
1. GenerationType.IDENTITY
- Used when the database automatically generates the primary key using auto-increment for each new record.
- Supported by MySQL, PostgreSQL, SQL Server, etc.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
package com.example.java_test_application;
@Entity
public class Employee {
// on the below line creating an employee id generated value with the strategy for generation type.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long empId;
private String employeeName;
private String employeeQualification;
}
Explanation: This defines an entity where the primary key is auto-generated by the database using auto-increment. It simplifies data insertion by removing manual ID handling.
2. GenerationType.SEQUENCE
- Used when a database sequence is used to generate unique primary key values in a controlled and efficient manner.
- Uses a database sequence (Oracle, PostgreSQL).
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@Entity
public class Department {
// on the below line creating a variable for department
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long departmentID;
// on the below line creating a variable for department
// name.
private String departmentName;
}
Explanation: This defines an entity where the primary key is generated using a database sequence. It provides efficient and controlled ID generation.
3. GenerationType.TABLE
- Uses a separate table to simulate sequences.
- Works across all databases but has performance overhead.
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long projectId;
private String projectName;
}
Explanation: This defines a Project entity where projectId is generated using a separate table to manage ID values. It is database-independent but may be slower due to extra table operations.
4. GenerationType.AUTO (Default)
Lets the JPA provider choose the best strategy based on the database.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long productId;
private String productName;
}
Explanation: This defines a Product entity where the primary key strategy is automatically chosen by the JPA provider based on the database. It provides flexibility and reduces manual configuration.
5. GenerationType.UUID
- GenerationType.UUID is not available in older JPA versions (like JPA 2.x).
- It is introduced in Jakarta Persistence 3.1 and supported by providers like Hibernate 6.
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id; // Stored as VARCHAR(36) or BINARY(16)
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String
userId; // e.g.,
// "123e4567-e89b-12d3-a456-426614174000"
private String username;
}
Explanation: This defines an entity where the primary key is generated as a UUID. It ensures global uniqueness, making it suitable for distributed systems.