The Wayback Machine - https://web.archive.org/web/20240701180815/https://www.geeksforgeeks.org/hibernate-generatedvalue-annotation-in-jpa/
Open In App

Hibernate – @GeneratedValue Annotation in JPA

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

@GeneratedValue annotation, the name itself suggests that it will generate something. This annotation is generally used in conjunction with @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. For marking the field property as a primary key of the entity we use @Id annotation. When we apply @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. The @GeneratedValue annotation provides us with different strategies for the generation of primary keys which are as follows : 

  1. GenerationType.IDENTITY: This strategy will help us to generate the primary key value by the database itself using the auto-increment column option. It relies on the database’s native support for generating unique values. 
  2. GenerationType.AUTO: This is a default strategy and the persistence provider which automatically selects an appropriate generation strategy based on the database usage.
  3. GenerationType.TABLE: This strategy uses a separate database table to generate primary key values. The persistence provider manages this table and uses it to allocate unique values for primary keys.
  4. GenerationType.SEQUENCE: This generation-type strategy uses a database sequence to generate primary key values. It requires the usage of database sequence objects, which varies depending on the database which is being used.

Example for @GeneratedValue Annotation

Example 1:

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:

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.



Similar Reads

Difference between JPA and Spring Data JPA
JPA provides specifications for persisting, reading, and maintaining data from Java objects to relational tables in a database. The JPA defines rules and principles for conducting standard interfaces. Spring data repository significantly reduces the extra code necessary to provide a data access layer for multiple persistence stores. Spring Data JPA
4 min read
Spring Data JPA - @Table Annotation
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
Spring Data JPA - @Id Annotation
Spring Boot is built on the top of the spring and contains all the features of spring. Spring also provides JPA and hibernate to increase the data manipulation efficiency between the spring application and the database. In very simple terms we can say JPA (Java persistence API) is like an interface and the hibernate is the implementation of the met
3 min read
Spring Data JPA - @Column Annotation
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
2 min read
Spring Data JPA - Attributes of @Column Annotation with Example
Spring Boot is built on the top of the spring and contains all the features of spring. Spring also provides JPA and hibernate to increase the data manipulation efficiency between the spring application and the database. In very simple terms we can say JPA (Java persistence API) is like an interface and the hibernate is the implementation of the met
3 min read
Spring Data JPA @Query Annotation with Example
Spring Data JPA or JPA stands for Java Persistence API, so before looking into that, we must know about ORM (Object Relation Mapping). So Object relation mapping is simply the process of persisting any Java object directly into a database table. @Query Annotation is used for defining custom queries in Spring Data JPA. When you are unable to use the
7 min read
Spring Data JPA - @Modifying Annotation
@Modifying annotation in Spring Data JPA allows modification queries such as update and delete and enhances the capabilities of the @Query annotation. It enables data transformation actions beyond simple data retrieval, ensuring transaction integrity and improving performance. The @Modifying annotation is used to enhance the @Query annotation so th
4 min read
Java - JPA vs Hibernate
JPA stands for Java Persistence API (Application Programming Interface). It was initially released on 11 May 2006. It is a Java specification that gives some functionality and standard to ORM tools. It is used to examine, control, and persist data between Java objects and relational databases. It is observed as a standard technique for Object Relat
4 min read
Spring Boot - Integrating Hibernate and JPA
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
Hibernate Example using JPA and MySQL
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, Hibernate does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is a Java framework that is used to develop persisten
4 min read
Practice Tags :
three90RightbarBannerImg