There are currently several options available for automatically generating IDs with Hibernate and JPA combined.
Example: @GeneratedValue(strategy = GenerationType.IDENTITY)
.
I am looking for documentation on how to choose the specific ID generation strategy that best suits each case.
1-) How are they different?
2-) Why is one chosen over another?
3-) Do all the strategies work if we talk about databases like Oracle, MySQL, PostgreSQL, ..?
AUTO
Indicates that the persistence provider should pick an appropriate strategy for the particular database.
IDENTITY
Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
SEQUENCE
Indicates that the persistence provider must assign primary keys for the entity using a database sequence.
This sequential number is specific to some databases and what it does is create a new number that is not repeated and return it. The difference is that there is no transaction isolation, so multiple transactions cannot get the same value. More info on this.
TABLE
Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.
A table that helps you take control. More info.
Much has to do with the application that you are going to use, but for me it has not been necessary to use another strategy than
IDENTITY
when the values are autonumeric. In the end hibernate does a good job and the goal is to avoid relying on DB technology, which is one of the features I work with Spring Boot for. I don't need to worry at the development stage about whether there will be changes to the DB manager or things like that that are usually a headache.