Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate UUID using spring annotations

I want to generate UUID in spring controller. I am new to this and I was exploring following

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

I do not want to associate this uuid with any database column/field but want it to be unique(I am not sure if this is possible)

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

I have also tried but value of corrId is still null

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

Am I doing something wrong here or my approach is completely wrong.

Thanks in advance!

like image 438
user327126 Avatar asked Feb 07 '17 10:02

user327126


1 Answers

You can simply define field this way:

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

Please read this post about UUID.randomUUID() and this one about @Transient.

like image 53
Tolledo Avatar answered Oct 17 '22 10:10

Tolledo