Issue
I am working on a spring boot application where I want to auto-generate the table Id (In MySQL) of my entity in a predefined format.
The format is like "AK"+6 digits (eg: AK000001, AK000002, AK000003...AK999999).
I am using strategy = GenerationType.AUTO
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
Please help me to auto-generate the Id of predefine format.
Solution
MySQL does not support using an auto-increment for this. See my answer to adding a kind of auto increment column to a mysql table
You will have to generate the id values yourself in Java code, and insert them explicitly. This means you have to use a number generator that is thread-safe and resistant to race conditions. If you have multiple app servers, it will have to be a service. For example, Memcached has an atomic number generation command.
Answered By - Bill Karwin
Answer Checked By - Robin (JavaFixing Admin)