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