如何使用spring anootations生成UUID

use*_*126 4 uuid spring annotations hibernate

我想在spring控制器中生成UUID.我是新手,我正在探索以下内容

@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String corrId;
Run Code Online (Sandbox Code Playgroud)

我不想将这个uuid与任何数据库列/字段相关联,但希望它是唯一的(我不确定这是否可行)

当我尝试打印String'corrId'的值时,它总是给我null

我也试过但是corrId的值仍然是null

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String corrId;
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么,或者我的做法是完全错误的.

提前致谢!

Tol*_*edo 6

您可以这样简单地定义字段:

@Transient
private UUID corrId = UUID.randomUUID();
Run Code Online (Sandbox Code Playgroud)

请阅读这篇文章关于UUID.randomUUID()和这个有关@Transient.