I am working on a SpringBoot application. The strict requirement I have is to generate server-side a numeric id for an entity and then persist it through the repository. Since each @Service is stateless and so a singleton, is the usage of an AtomicLong a good way to implement it?
Here is my code.
In the service, I have this field
private final AtomicLong currentId = new AtomicLong();
In the service in the called method, I use the repository to persist data in this way:
myEntityRepository.save(MyEntity.builder()
.id(currentIdNumber.incrementAndGet())
//.... defining other fields
.build());
If the code I provided is not enough to answer me, I will happily edit my question according to the comments.