Last Updated : 05 Apr, 2025
The @GeneratedValue annotation, the name itself suggests that it will generate something. This annotation is generally used in conjunction with the @Id annotation to automatically generate unique values for primary key columns within our database tables. When creating an entity class, we have to specify a primary key for that entity. To mark the field property as a primary key of the entity, we use the @Id annotation. When we apply the @GeneratedValue annotation to our primary key field or property. It will instruct Hibernate to automatically generate a unique value for that field during the process of persisting the entity into the database.
Generation StrategiesThe @GeneratedValue annotation provides us with different strategies for the generation of primary keys, which are as follows :
1. GenerationType.IDENTITY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
2. GenerationType.SEQUENCE
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
3. GenerationType.TABLE
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
4. GenerationType.AUTO (Default)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
5. GenerationType.UUID
Example for @GeneratedValue Annotation@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id; // Stored as VARCHAR(36) or BINARY(16)
Example 1: Using IDENTITY
Java
package com.example.java_test_application;
// on the below line creating an entity class for the class
// of Employee.
@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:
In the above example, we are considering an Employee entity. Inside this entity, we are marking empID with @Id annotation to indicate it as a primary key. The @GeneratedValue annotation with GenerationType Identity indicates that the primary key will be generated automatically by the database. The @GenaratedValue annotation simplifies generating unique primary key values and allows persistence providers to handle this task automatically, reducing boilerplate code needed for managing the primary keys in the database.
Example 2: Using SEQUENCE
Java
// on the below line creating an entity class for the class
// of Department.
@Entity
public class Department {
// on the below line creating a variable for department
// id.
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long departmentID;
// on the below line creating a variable for department
// name.
private String departmentName;
}
Explanation:
In the above example, we are considering a Department entity. Inside this entity, we are marking departmentID with @Id annotation to indicate it as a primary key. The @GeneratedValue annotation with GenerationType SEQUENCE will generate primary key values which require the usage of the database sequence objects.
Example 3: Using UUID
Java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String userId; // e.g., "123e4567-e89b-12d3-a456-426614174000"
private String username;
}
Generates a unique string ID (no database sequence needed).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4