Given a table in a mySQL database looks like this with the following columns:
| id | name | reference|
- id is a big-int auto-generated when persisting
- name is a varchar
- reference is a varchar
I am currently using spring-boot with spring-jpa.
Is there a way when I am persisting this entity to have the reference column be auto-generated in a custom way only when it is null. For example:
INSERT INTO my_table(name,reference) VALUES('John', 'MREF-System-0940');
Will produce a result as follows
| id | name | reference |
| 1 | John | MREF-System-0940 |
But when I execute the following
INSERT INTO my_table(name) VALUES('John');
Will produce a result as follows (Where reference is based as 'REF-{name}-{sequence}' where sequence could be the id or a predefined persistent sequence)
| id | name | reference |
| 2 | John | REF-John-2 |
It should auto-generate the reference column reference when it detects that column is null when persisting the entity.
If possible could we append the generated id column at the end? If not, is there a feasible way of auto-generating a reference as such.
Meaning if we do
INSERT INTO my_table(name) VALUES('Bob');
It might produce a result as follows
| id | name | reference |
| 3 | Bob | REF-Bob-001 |