0

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.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46

1 Answers1

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?

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74