“自动生成的PK作为ID"和“以字符串形式自动生成的UUID".在单个实体中的Spring Data JPA [英] "Autogenerated PK as Id" and "Autogenerated UUIDs as String" in a single Entity Spring Data JPA

查看:52
本文介绍了“自动生成的PK作为ID"和“以字符串形式自动生成的UUID".在单个实体中的Spring Data JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是对的扩展,和我的自己的上一个问题.

在也加载了其他读数之后,我的实体中包含以下内容:

  @Id@GeneratedValue(策略= GenerationType.IDENTITY)@Column(名称="id")私人Long ID;@Column(名称="useruuid")@GenericGenerator(名称="uuid",策略="uuid4")私人String useruuid; 

问题1:以上声明是否有效?我们大家都知道@Id是肯定的.问题更多地集中在 @GenericGenerator(name ="uuid",strategy ="uuid4").含义在已经具有 @GeneratedValue 的实体中生成UUID.可以接受吗?我尝试过了,而且行得通.

问题2:如果上面的是,那么生成的UUID(字符串)对于整个数据库还是仅对于实体是唯一的?我在这里寻求帮助,因为我将在同一数据库的其余实体中使用相同的模式.

解决方案

是的 @GenericGenerator(name ="uuid",strategy ="uuid4")是可以接受的,并且是完全正确的.

现在要问第二个问题,生成的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆