@GeneratedValue annotation
@GeneratedValue annotation is used to generate primary key values for entities.
This annotation is applied over a primary key property or field in an entity class.
Generally, this field has an @Id
annotation, which indicates that this field is a primary key for this entity.
The
GeneratedValue
annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with theId
annotation.
When you annotate a field with @GeneratedValue
, you’re telling Spring Boot to automatically generate unique values for that field. Example,
import javax.persistence.*; @Entity public class Employee { @Id @GeneratedValue private Long id; private String name; // getters and setters }
There are different ways in which @GeneratedValue
can generate the values for primary key field and these ways are called strategies.
A strategy is provided with strategy
attribute and we will be discussing various strategies next.
1. Default strategy
When @GeneratedValue
is used without any attribute, it refers to the default strategy.
This means that the primary key generation will be handled by the persistence provider such as Hibernate and this is generally dependent on the database type.
So, for different databases, the strategy might be different but this is taken care by the persistence provider. Example,
import javax.persistence.*; @Entity public class Employee { @Id @GeneratedValue private Long id; private String name; // getters and setters }
Behind the scenes it is same as @GeneratedValue
(strategy = GenerationType.AUTO)
If you change the datatype of id field to java.util.UUID
, then primary key will be of the form c84153ed-ad46-40f2-b019-d4615598b335.
2. Identity strategy
This strategy relies on the database to generate the primary key when a new record is inserted.
The primary key column in this case should be auto increment and database automatically generates the primary key by incrementing a value from previous inserted record. Example,
import javax.persistence.*; @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String name; // getters and setters }
Identity strategy may not work well with batch inserts, which are used to insert multiple records in a single database transaction.
Since the primary key values are generated by the database, it may not be possible to obtain the generated values for each record in the batch until the transaction has been committed.
This strategy generates the primary key values using a database sequence.
A database sequence generates a sequence of unique integer values.
Each time a value is requested from the sequence, it increments its current value and returns the new value.
To use this strategy, we need to create a sequence at the database level using CREATE SEQUENCE
query, and then use it in the entity class with @GeneratedValue
annotation along with @SequenceGenerator
annotation. Example,
@Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="employee_seq") @SequenceGenerator(name="employee_seq", sequenceName="EMPLOYEE_SEQ", allocationSize=1) private Long id; private String name; // getters and setters }
In this example, we are using sequence generator using GenerationType.SEQUENCE and defining the name of sequence generator to be used as employee_seq.
Name of sequence created at database is provided using sequenceName
attribute and its value is EMPLOYEE_SEQ , which means that there should be a sequence with this name created at database.
allocationSize
attribute defines the increment amount when generating the next value. Its default value is 50.
4. Table strategy
This strategy maintains a separate database table to generate primary key values and hence, its name.
This strategy is defined using @TableGenerator
annotation, which contains details such as name of database table to use, its schema, primary key column etc. Example,
@Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.TABLE, generator="employee_gen") @TableGenerator(name="employee_gen", table="id_gen", pkColumnName="gen_name", valueColumnName="gen_val", allocationSize=1) private Long id; private String name; // getters and setters }
We are defining the strategy with strategy=GenerationType.TABLE
.
Here,
employee_gen is the name of sequence and this should be provided in generator
attribute of @GeneratedValue
.
It is mandatory.
id_gen is the name of table that will be used to maintain primary keys. It is optional.
gen_name is the name of primary key column. It is optional.
gen_val is the name of column that will store the last value generated. It is optional.
allocationSize
defines the increment amount when generating the next value. Its default value is 50 and it is optional.
@GeneratedValue
annotation in Spring boot and JPA.This annotation can be used to generate unique primary key values for entity classes using various strategies.
Hope the article was useful.