4

Inspired by the suggestion given here - JPA Entity class giving error with 2 @GeneratedValue fields, my question is the OPPOSITE of this - How to generate uuids without dashes

I am using H2 DB and have this in my model:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "useruuid")
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid4")
    private UUID userUUID; 

And in my controller it goes like this:

    @PostConstruct
    private void postConstruct() {
        AppUser appUser = new AppUser(1l, UUID.randomUUID());
        appUserJPARepository.save(appUser);
    }

Now when my Spring Boot app starts my H2 DB-Console shows this:

ID      USERUUID  
1       25b9b7f391d94825b349866fe9a9077c

Question: How do I get hyphens in the DB? So that my uuid in the DB will be - 25b9b7f3-91d9-4825-b349-866fe9a9077c

I fiddled with @GenericGenerator(name = "uuid4", strategy = "uuid4") uuid(1) to uuid5 but got the same result. What is going on here and what I am doing wrong or what should I do to get hyphens in the DB? Any help or related info/links relevant to this will be greatly appreciated.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • What's the column type ? The dashes are only a cosmetic feature, so if you are storing the UUID as byte array you won't get any. If you store it as a text/varchar you'll get the dashes, but that's a pretty inefficient way to store uuids – Svetlin Zarev Jun 25 '20 at 06:13
  • I have gone through this - https://tomharrisonjr.com/uuid-or-guid-as-primary-keys-be-careful-7b2aa3dcb439. Whats your suggestion @Svetlin? I mean how should I use it? I am clueless. – Ajay Kumar Jun 25 '20 at 06:16
  • 8
    H2 has a dedicated UUID type which is a 128 bit value. The dashes are purely cosmetic and it appears H2's console is not adding them for you. Since you are using the UUID as an opaque random external unique identifier, you don't need it to have dashes and when the Java UUID is viewed it will have dashes as the Java UUID class adds them for presentation. – Simon G. Jun 25 '20 at 06:27
  • 2
    Also - since you are using a UUID for security reasons, make sure you use "uuid4" not the others. You can tell what type of UUID it is by examining the bits. Type 1 reveals where and when the record was created. Type 4 is random and so secure. The other types require additional information which may be predictable and hence produce the predictable IDs you are trying to avoid. – Simon G. Jun 25 '20 at 06:34
  • Perfect. Got it Simon. Thanks for your valuable suggestion and help. – Ajay Kumar Jun 25 '20 at 13:54

1 Answers1

0

You can just remove

@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid4")

and the code will looks like

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "useruuid")
@GeneratedValue
private UUID userUUID; 
Miguel Galindo
  • 111
  • 1
  • 3