Tags: Spring Boot
The four generation types in Java Spring (Hibernate) are AUTO, IDENTITY, SEQUENCE, and TABLE. Here are the differences between them:
-
AUTO:
-
The AUTO generation type is the default and allows the persistence provider (e.g., Hibernate) to choose the generation strategy[^3^].
-
When using Hibernate as the persistence provider, AUTO typically defaults to GenerationType.SEQUENCE[^9^].
-
Example:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; // ... }
-
-
IDENTITY:
-
The IDENTITY generation type assigns the task of primary key generation to the database[^13^].
-
With this strategy, the database automatically generates the primary key value when inserting a row, so there is no need to specify a value for the ID[^13^].
-
Example:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; // ... }
-
-
SEQUENCE:
-
The SEQUENCE generation type relies on a database sequence to generate unique IDs[^13^].
-
It queries the database for the next sequence value and uses it to insert a row with the corresponding sequence ID[^13^].
-
Example:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Integer id; // ... }
-
-
TABLE:
-
The TABLE generation type uses a database table to generate unique IDs[^13^].
-
It inserts a row into a specific table that maintains a counter for each entity type and uses the generated value as the primary key[^13^].
-
Example:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "table-generator") @TableGenerator(name = "table-generator", table = "ids", pkColumnName = "seq_id", valueColumnName = "seq_value") private Integer id; // ... }
-
Each generation type has its advantages and considerations. The choice of generation type depends on factors such as database support, performance requirements, and portability concerns[^13^].
GenerationType.IDENTITY relies on an auto-incrementing column in the database, which may cause conflicts if multiple threads try to insert a new row at the same time.
To handle this, you can use GenerationType.SEQUENCE, which uses a database sequence to generate IDs. This strategy is thread-safe because the database ensures that each new ID is unique.