@GeneratedValue - JPA Annotation | GenerationType AUTO, IDENTITY, SEQUENCE and TABLE

@GeneratedValue - This annotation specifies the generation strategies for the values of primary keys.

The @GeneratedValue annotation specifies that the entity identifier value is automatically generated using an identity column, a database sequence, or a table generator. Hibernate supports the @GeneratedValue mapping even for UUID identifiers.

We can generate the identifiers in different ways which are specified by the @GeneratedValue annotation.
We can choose from four id generation strategies with the strategy element. The value can be AUTOTABLESEQUENCE, or IDENTITY.

@GeneratedValue - JPA Annotation Examples

1. GenerationType.AUTO

The GenerationType.AUTO is the default generation type and lets the persistence provider choose the generation strategy.
For example, consider we have a Student JPA entity class with GenerationType.AUTO as generation type:
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    public Student() {

    }

   // getter and setters
}

2. GenerationType.IDENTITY

The GenerationType.IDENTITY is the easiest to use but not the best one from a performance point of view. It relies on an auto-incremented database column and lets the database generate a new value with each insert operation. 

For example, consider we have a Student JPA entity class with GenerationType.IDENTITY as a generation type:
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    public Student() {

    }

   // getter and setters
}

3. GenerationType.SEQUENCE

The GenerationType.SEQUENCE is to generate primary key values and uses a database sequence to generate unique values.
For example, consider we have a Student JPA entity class with GenerationType.SEQUENCE as generation type:
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    public Student() {

    }

   // getter and setters
}

4.GenerationType.TABLE

The GenerationType.TABLE gets only rarely used nowadays. It simulates a sequence by storing and updating its current value in a database table which requires the use of pessimistic locks that put all transactions into sequential order. This slows down your application, and you should, therefore, prefer the GenerationType.SEQUENCE, if your database supports sequences, which most popular databases do.

For example, consider we have a Student JPA entity class with GenerationType.TABLE as generation type:
@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    public Student() {

    }

   // getter and setters
}
You can use the @TableGenerator annotation in a similar way as the already explained @SequenceGenerator annotation to specify the database table which Hibernate shall use to simulate the sequence.

References

https://www.javaguides.net/2018/12/hibernatejpa-primary-key-generation-stratergies.html.

https://javaee.github.io/javaee-spec/javadocs/javax/persistence/GeneratedValue.html.


Comments