javaspringhibernateuuidgenerator

UuidGenerator Bean Type Mismatch?


Because strategy and UUIDGenerator is deprecated, this is the new UUID Generator:

@GenericGenerator(
            name = "UUID", 
            type = org.hibernate.id.uuid.UuidGenerator.class
    )

When using this, I get the following error:

Parameter 0 of constructor in org.hibernate.id.uuid.UuidGenerator required a bean of type 'org.hibernate.annotations.UuidGenerator' that could not be found

I wasn't able to find anything corresponding to this in the documentation.

For full disclosure, this is the whole annotation block used:

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "UUID")
@GenericGenerator(name = "UUID", type = org.hibernate.id.uuid.UuidGenerator.class)
@Column(...)

Solution

  • I really suggest you to use @UuidGenerator instead of

    @GeneratedValue(strategy = GenerationType.AUTO, generator = "UUID")
    @GenericGenerator(name = "UUID", type = org.hibernate.id.uuid.UuidGenerator.class)
    

    Something like this:

      @Id
      @UuidGenerator
      private UUID id; // or String
    

    Because if you are going to check java docs of the annotation you will see that @UuidGenerator use @IdGeneratorType( org.hibernate.id.uuid.UuidGenerator.class ) that you need.

    About the problem i really dont know where can be the problem but the solution that i suggest will help you to do this replacement also when specifying @UuidGenerator, we can choose the concrete version of UUID to generate.

    And also some motivation to use it is described into 8.5. User-defined generators

    These APIs are new in Hibernate 6, and supersede the classic IdentifierGenerator interface and @GenericGenerator annotation from older versions of Hibernate.

    @UuidGenerator A more flexible generator for RFC 4122 UUIDs