Hey guys in this post, we will discuss everything you need to know about Spring data JPA ID generators and their examples.
Table of Contents
Overview
ID generators are used to generate ID automatically for the primary key column when the new record is inserted into the table.
JPA provides 4 different types of ID Generation strategies –
- GenerationType.AUTO
- GenerationType.IDENTITY
- GenerationType.SEQUENCE
- GenerationType.TABLE
Let’s look at this one by one in detail
GenerationType.AUTO
As the name suggests, Data JPA automatically checks for the ID generation strategy based on the underlying database. It picks either one of the strategies such as GenerationType.IDENTITY
, GenerationType.SEQUENCE
or GenerationType.TABLE
to generate the ID value for the primary key column.
@Entity
@Table(name="tbl_employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//other fields
}
GenerationType.IDENTITY
Data JPA will rely on the auto_increment
column to generate the ID. When we create a primary key, we will also configure it to be an auto_increment
field. Based on the auto_increment
value, the ID will be generated when a new record is inserted. We use this strategy when we are working with MySQL or SQLite databases.
@Entity
@Table(name="tbl_employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//other fields
}
GenerationType.SEQUENCE
We can create sequences in the database, where we define the custom login to generate the value later we can use that value as the ID. Data JPA can also use a sequence as the generation strategy where we can tell JPA which sequence should use in the database. We use this strategy when we are working with Oracle or PostgreSQL database.
@Entity
@Table(name="tbl_employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
//other fields
}
GenerationType.TABLE
As the name suggests, the database creates a separate table and the data JPA will insert the value for the table also uses the same value to insert the record into the database table.
@Entity
@Table(name="tbl_employee")
public class Employee {
@TableGenerator(name = "generate_employee",
table = "tbl_generate_id",
pkColumnName = "generate_name",
valueColumnName = "generate_value",
allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "generate_employee")
private Long id;
//other fields
}
Custom generator
We can also use a Custom generator to generate the ID instead of using built-in strategies. In order to do that, we need to create a class and implements the IdentifierGenerator
interface and we need to override the only method generate()
public class CustomIDGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
//logic to generate id
return null;
}
}
Next, we need to provide the fully qualified class name in.bushansirgur.springboot.entity.custom.CustomIDGenerator
to the @GenericGenerator
annotation of the strategy option
@Entity
@Table(name="tbl_employee")
public class Employee {
@Id
@GenericGenerator(name="custom_emp", strategy = "in.bushansirgur.springboot.entity.custom.CustomIDGenerator")
@GeneratedValue(generator = "custom_emp")
private Long id;
//other fields
}