现在要问第二个问题,生成的UUID在整个数据库中是唯一的.实际上,它是全局唯一的,因此UUID称为全局唯一标识符".(GUID).除非您多年连续每秒生成十亿个UUID,否则您不会看到两个UUID版本4实现冲突.阅读: UUID有多独特? My question is an extension to this, this and my own previous question.After loads of other readings too, I have the following in my Entity:@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private Long id;@Column(name = "useruuid")@GenericGenerator(name = "uuid", strategy = "uuid4")private String useruuid;Question 1: Are above declarations valid? @Id is for sure yes as we all know it. The question is more focussed on @GenericGenerator(name = "uuid", strategy = "uuid4"). Meaning Generating UUIDs in the Entity which already has @GeneratedValue. Is it acceptable? I tried it and it works.Question 2: If above is yes, then would the generated UUID as String will be unique for entire DB or just for the Entity only? I am seeking help here as I would use the same pattern in my rest of the Entities in the same Database. 解决方案 Yes @GenericGenerator(name = "uuid", strategy = "uuid4") is acceptable and totally correct.Now coming to your second question, UUID generated is unique throughout entire DB. In fact, it's globally unique hence UUID is called "globally unique identifiers" (GUIDs).You will not see two UUID version 4 implementations collide unless you generate a billion UUIDs every second for many years.Read: How unique is UUID? 这篇关于“自动生成的PK作为ID"和“以字符串形式自动生成的UUID".在单个实体中的Spring Data JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 19:46