Hibernate @GeneratedValue Annotation in JPA (original) (raw)

Last Updated : 23 Apr, 2026

@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.

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

Java `

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

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

Java `

@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

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

Java `

@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;

Java `

@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

@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id; // Stored as VARCHAR(36) or BINARY(16)

Java `

@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.