Using the new type() for UUIDGenerator instead of strategy?
Asked Answered
T

3

8

According to the documentation, the following usage is deprecated:

@GenericGenerator(
            name = "UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )

One shall use the new type()! But how can I transform my above statement to do the same with the type()? Just providing the hibernate UUIDGenerator doesn't work.

Tirrell answered 19/7, 2023 at 16:12 Comment(3)
From the doc, strategy is a string, type is a class. Have you tried type = org.hibernate.id.UUIDGenerator.class ?Payee
I believe that should be either @org.hibernate.annotations.UuidGenerator or @GenericGenerator(name = "UUID", type = org.hibernate.id.uuid.UuidGenerator.class)Eductive
@Payee @Andrey B. Panfilov Nvm, I used: @GenericGenerator(name = "UUID", type = org.hibernate.id.uuid.UuidGenerator.class) and got: org.hibernate.MappingException: Could not instantiate id generator for all classes using your new approach?Tirrell
T
13

As the two comments above suggest, type is now of type class. Thus the fix is simple:

@GenericGenerator(
            name = "UUID",
            type = org.hibernate.id.uuid.UuidGenerator.class
    )
Tirrell answered 19/7, 2023 at 18:32 Comment(3)
Seems good way two replace "strategy" property, but when I user that I get another error concerning dependency injection "Parameter 0 of constructor in org.hibernate.id.uuid.UuidGenerator required a bean of type 'org.hibernate.annotations.UuidGenerator' that could not be found".Norvil
@Oscar same. Did you find way to fix it?Kaete
@Denys Bondarenko This has been answered in #77128992 in case you're curious and still facing the same issue you did earlier.Tirrell
M
11

Use the new annotation @UuidGenerator

Old solution:

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

New solution (since hibernate 6.2):

@Id
@UuidGenerator
private UUID id;
Mic answered 12/1, 2024 at 21:24 Comment(1)
take care: dev.to/mcadariu/using-uuids-as-primary-keys-3e7aMonohydroxy
S
0
@Id
@UuidGenerator(style = UuidGenerator.Style.TIME)
private UUID id;
Shulins answered 21/9, 2024 at 10:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.