“Autogenerated PK as Id” and “Autogenerated UUIDs as String” in a single Entity Spring Data JPA

随声附和 提交于 2021-01-29 09:56:38

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/62583209/autogenerated-pk-as-id-and-autogenerated-uuids-as-string-in-a-single-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!