JPA sequence for serial number

I wonder if there is a clean (or native) way to generate the sequence for the table in order to use it as a serial number. It should be consistent (1, 2, 3 ... etc.) and avoid any possible problems with the race / transaction (if several users try to save this time). It does not have to be the primary key.

@Id private Long id; private Long serialNumber; 

thanks.

+4
source share
4 answers

I wonder if there is a clean (or native) way to generate the sequence for the table in order to use it as a serial number.

As far as I know, no, not for a field not Id .

But you can use the selected object with the appropriate generator strategy and save a new instance when you want to get the next identifier from it.

Another option is to model the strategy of the TABLE generator and use your own SQL to read the next identifier and increase it.

+2
source

@id @GeneratedValue (strategy = IDENTITY)?

+1
source

DataNucleus supports the use of @GeneratedValue in non-Id fields.

+1
source

Source: https://habr.com/ru/post/1315801/


All Articles