I have an entity which contain an identity column which i want it to be automatically generated. So the column is formed like that: DDMMYYYYN so DD is day, MM is month and YYYYY is the year and N is an autoincremnted number which increment each insert and reset to 0 if it is a new year. Is there a way to do this identity strategy in Jpa. There is a solution that i want to avoid: i can get the last record compare the date and increment N. I am using jpa, spring boot as framework, and postgres as database
Asked
Active
Viewed 142 times
0
-
Why do you want to do this? The format looks strange and it is not sortable because you have the da first. – Simon Martinelli Jun 16 '22 at 14:04
-
it is a key of the entity – fbm fatma Jun 16 '22 at 14:12
-
Yes but why so complicated? You could use a simple sequence – Simon Martinelli Jun 16 '22 at 14:20
-
the client want it like that – fbm fatma Jun 16 '22 at 14:21
-
Does this answer your question? [How to generate Custom Id using hibernate while it must be primary key of table](https://stackoverflow.com/questions/31158509/how-to-generate-custom-id-using-hibernate-while-it-must-be-primary-key-of-table) – Simon Martinelli Jun 16 '22 at 14:31
-
There are times that as a professional you need to tell the customer what they want is a very bad plan; this is one of then. You also **must** tell them why and offer an alternative. In this case it will not perform well in a multi-user environment as you will need to lock the table for every insert to avoid duplicating *n*. The identity is not sort-able; Jan 31 (3101) comes well after Feb 01 (0102). A better approach store the timestamp in ms (still can get duplicates, but less chance). You can then always create a view to generate *n* from the timestamp. Just one of many many possibilities. – Belayer Jun 17 '22 at 23:40