添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

  • 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.
  • GenerationType.AUTO : This is a default strategy and the persistence provider which automatically selects an appropriate generation strategy based on the database usage.
  • 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.
  • 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:

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

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

    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.

    Last Updated : 21 Jun, 2023 Like Article
    @GeeksforGeeks, Sanchhaya Education Private Limited , All rights reserved We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
    You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.