'GenerationType.AUTO vs GenerationType.IDENTITY in hibernate

Currently, we are using MySQL as a database and we use

@Generated Value(strategy = GenerationType.IDENTITY)

It's working perfectly in certain situations we need to migrate our database to Oracle at that time it's not working properly. If anyone knows what's the actual difference is present behind this and how it's working?



Solution 1:[1]

Quoting Java Persistence/Identity and Sequencing:

Identity sequencing uses special IDENTITY columns in the database to allow the database to automatically assign an id to the object when its row is inserted. Identity columns are supported in many databases, such as MySQL, DB2, SQL Server, Sybase and Postgres. Oracle does not support IDENTITY columns but they can be simulated through using sequence objects and triggers.

so I prefer to use SEQUENCE instead

Sequence objects use special database objects to generate ids. Sequence objects are only supported in some databases, such as Oracle, DB2, and Postgres. Usually, a SEQUENCE object has a name, an INCREMENT, and other database object settings. Each time the .NEXTVAL is selected the sequence is incremented by the INCREMENT.

Example :

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMP_SEQ")
    @SequenceGenerator(name="EMP_SEQ", sequenceName="EMP_SEQ", allocationSize=100)
    private long id;
    ...
}

Solution 2:[2]

I would recommend reading this article for fast and thorough understanding of 3 most common generation types namely AUTO , IDENTITY and SEQUENCE

COMMON GENERATION TYPES DIFFERENCE

Solution 3:[3]

If anyone is looking for the ans - GenerationType.IDENTITY vs GenerationType.SEQUENCE vs GenerationType.AUTO then this is a good blog- link

Solution 4:[4]

Im using JPA and Oracle 11g, the solution that worked for me is the following

package com.example.springsocial.model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "rol", uniqueConstraints = {
        @UniqueConstraint(columnNames = "name")
})
public class Rol {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="rol_sequence")
    @SequenceGenerator(name="rol_sequence", sequenceName="rol_sequence", allocationSize=100)
    private Long id;

    @Column(nullable = false)
    private String name;

    private Date createdAt;
    @Column(nullable = true)
    private Date updatedAt;
    @Column(nullable = true)
    private Integer createdBy;
    @Column(nullable = true)
    private Integer updatedBy;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Integer getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(Integer updatedBy) {
        this.updatedBy = updatedBy;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahmad Al-Kurdi
Solution 2 developerdhairya
Solution 3 Md.Habibur Rahman
Solution 4 Jorge Santos Neill