How to generate UUID using spring anootations

I want to generate a UUID in a spring controller. I am new to this and I studied the following

@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String corrId;

I don't want to bind this uuid to any database column / field, but I want it to be unique (I'm not sure if this is possible)

When I try to print the String 'corrId' value, it always gives me null

I also tried, but the value of corrId is still null

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String corrId;

I'm doing something wrong here, or my approach is completely wrong.

Thanks in advance!

+4
source share
1 answer

You can simply define the field this way:

@Transient
private UUID corrId = UUID.randomUUID();

UUID.randomUUID() @Transient.

+6

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


All Articles