@@ -306,6 +306,7 @@ The most important piece of information here is the specified `jakarta.persisten
306306`IDENTITY`:: Indicates that database IDENTITY columns will be used for primary key value generation. See <<identifiers-generators-identity>>.
307307`SEQUENCE`:: Indicates that database sequence should be used for obtaining primary key values. See <<identifiers-generators-sequence>>.
308308`TABLE`:: Indicates that a database table should be used for obtaining primary key values. See <<identifiers-generators-table>>.
309+ `UUID`:::Indicates that UUID generation should be used. See <<identifiers-generators-uuid>>
309310
310311[[identifiers-generators-auto]]
311312==== Interpreting AUTO
@@ -314,7 +315,7 @@ How a persistence provider interprets the AUTO generation type is left up to the
314315
315316The default behavior is to look at the Java type of the identifier attribute, plus what the underlying database supports.
316317
317- If the identifier type is UUID, Hibernate is going to use a <<identifiers-generators-uuid, UUID identifier >>.
318+ If the identifier type is UUID, Hibernate is going to use a <<identifiers-generators-uuid, UUID generator >>.
318319
319320If the identifier type is numeric (e.g. `Long`, `Integer`), then Hibernate will use its `SequenceStyleGenerator` which
320321resolves to a SEQUENCE generation if the underlying database supports sequences and a table-based generation otherwise.
@@ -500,38 +501,122 @@ include::{extrasdir}/id/identifiers-generators-table-configured-persist-example.
500501[[identifiers-generators-uuid]]
501502==== Using UUID generation
502503
503- As mentioned above, Hibernate supports UUID identifier value generation.
504- This is supported through its `org.hibernate.id.UUIDGenerator` id generator.
504+ Hibernate offers 2 flavors of support for UUID generation -
505505
506- NOTE:: `org.hibernate.id.UUIDGenerator` is an example of `@IdGeneratorType` discussed in <<identifiers-generators-IdGeneratorType>>
506+ 1. using `org.hibernate.id.uuid.UuidGenerator`, which can be configured using the `org.hibernate.annotations.UuidGenerator` annotation.
507+ 2. using `org.hibernate.id.UUIDGenerator`, which can be configured using the `@GenericGenerator` annotation. Note that this approach is deprecated.
507508
509+ For legacy reasons, `org.hibernate.id.UUIDGenerator` is used when the generator is implicit (or explicitly requested via `@GenericGenerator`).
508510
509- `UUIDGenerator` supports pluggable strategies for exactly how the UUID is generated.
510- These strategies are defined by the `org.hibernate.id.UUIDGenerationStrategy` contract.
511- The default strategy is a version 4 (random) strategy according to IETF RFC 4122.
512- Hibernate does ship with an alternative strategy which is a RFC 4122 version 1 (time-based) strategy (using IP address rather than mac address).
511+ [NOTE]
512+ ====
513+ Future versions of Hibernate will drop support for `org.hibernate.id.UUIDGenerator` and the following 3 examples
514+ will then use `org.hibernate.id.uuid.UuidGenerator`.
515+ ====
516+
517+ [[example-identifiers-generators-uuid-implicit]]
518+ .Implicit UUID generation
519+ ====
520+ [source,java]
521+ ----
522+ include::{example-dir-identifier}/uuid/implicit/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
523+ ----
524+ ====
525+
526+ [[example-identifiers-generators-uuid-implicit2]]
527+ .Another example of implicit UUID generation
528+ ====
529+ [source,java]
530+ ----
531+ include::{example-dir-identifier}/uuid/implicit2/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
532+ ----
533+ ====
534+
535+ [[example-identifiers-generators-uuid-implicit3]]
536+ .Implicit UUID generation with String
537+ ====
538+ [source,java]
539+ ----
540+ include::{example-dir-identifier}/uuid/implicit3/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
541+ ----
542+ ====
543+
544+ The second approach, using `org.hibernate.id.uuid.UuidGenerator`, is much more flexible and usable
545+ because it builds on top of the <<identifiers-generators-IdGeneratorType,@IdGeneratorType>> support.
546+
547+ To use (and optionally configure) this strategy, use the `org.hibernate.annotations.UuidGenerator` annotation.
548+
549+ By default, Hibernate uses a random (IETF RFC 4122 version 4) generation.
550+
551+ [[example-identifiers-generators-uuid-random]]
552+ .Random UUID generation
553+ ====
554+ [source,java]
555+ ----
556+ include::{example-dir-identifier}/uuid/random/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
557+ ----
558+ ====
559+
560+ [[example-identifiers-generators-uuid-random2]]
561+ .Random UUID generation, with explicit style
562+ ====
563+ [source,java]
564+ ----
565+ include::{example-dir-identifier}/uuid/random2/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
566+ ----
567+ ====
568+
569+ [[example-identifiers-generators-uuid-random3]]
570+ .Random UUID generation, with String
571+ ====
572+ [source,java]
573+ ----
574+ include::{example-dir-identifier}/uuid/random3/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
575+ ----
576+ ====
513577
514- [[identifiers-generators-uuid-mapping-example]]
515- .Implicitly using the random UUID strategy
578+ Hibernate also comes with simplified support for a time-based (IETF RFC 4122 version 1, variant2) generation.
579+
580+ [[example-identifiers-generators-uuid-time]]
581+ .Time-based UUID generation
582+ ====
583+ [source,java]
584+ ----
585+ include::{example-dir-identifier}/uuid/time/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
586+ ----
587+ ====
588+
589+ [[example-identifiers-generators-uuid-time2]]
590+ .Time-based UUID generation using String
516591====
517592[source,java]
518593----
519- include::{example-dir-identifier}/UuidGeneratedValueTest .java[tag=identifiers-generators-uuid-mapping-example , indent=0]
594+ include::{example-dir-identifier}/uuid/time2/Book .java[tag=example- identifiers-generators-uuid-implicit , indent=0]
520595----
521596====
522597
523- To specify an alternative generation strategy, we'd have to define some configuration via `@GenericGenerator`.
524- Here we choose the RFC 4122 version 1 compliant strategy named `org.hibernate.id.uuid.CustomVersionOneStrategy `.
598+ For even more flexibility, Hibernate also offers the ability to plug in custom algorithms for creating the UUID value
599+ by specifying an implementation of `org.hibernate.id.uuid.UuidValueGenerator `.
525600
526- [[identifiers-generators-custom- uuid-mapping-example ]]
527- .Implicitly using the random UUID strategy
601+ [[example- identifiers-generators-uuid-custom ]]
602+ .Custom UUID generation
528603====
529604[source,java]
530605----
531- include::{example-dir-identifier}/UuidCustomGeneratedValueTest .java[tag=identifiers-generators-custom- uuid-mapping-example , indent=0]
606+ include::{example-dir-identifier}/uuid/custom/Book .java[tag=example- identifiers-generators-uuid-implicit , indent=0]
532607----
533608====
534609
610+ [[example-identifiers-generators-uuid-custom2]]
611+ .Custom UUID generation using String
612+ ====
613+ [source,java]
614+ ----
615+ include::{example-dir-identifier}/uuid/custom2/Book.java[tag=example-identifiers-generators-uuid-implicit, indent=0]
616+ ----
617+ ====
618+
619+
535620[[identifiers-generators-optimizer]]
536621==== Optimizers
537622
0 commit comments