From 8203c7ff79f118d80dfef68ff2b00829fc03eb17 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 16 Jan 2025 17:12:28 +0100 Subject: [PATCH] HHH-4309 Add documentation explaining how overriding generators work --- .../chapters/domain/identifiers.adoc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/documentation/src/main/asciidoc/userguide/chapters/domain/identifiers.adoc b/documentation/src/main/asciidoc/userguide/chapters/domain/identifiers.adoc index a1ced8c4c07f..55655fd9f68b 100644 --- a/documentation/src/main/asciidoc/userguide/chapters/domain/identifiers.adoc +++ b/documentation/src/main/asciidoc/userguide/chapters/domain/identifiers.adoc @@ -4,6 +4,7 @@ :core-project-dir: {root-project-dir}/hibernate-core :core-test-base: {core-project-dir}/src/test/java/org/hibernate/orm/test :example-dir-identifier: {core-test-base}/mapping/identifier +:example-dir-generated: {core-test-base}/mapping/generated :example-dir-associations: {core-test-base}/associations :jpaJavadocUrl: https://javadoc.io/doc/jakarta.persistence/jakarta.persistence-api/latest/jakarta.persistence :extrasdir: extras @@ -392,6 +393,24 @@ include::{example-dir-identifier}/SequenceGeneratorConfiguredTest.java[tag=ident Again the mapping specifies `explicit_product_sequence` as the physical sequence name, but it also specifies an explicit allocation-size ("increment by"). +The scope of the generator name can be controlled with the <> +configuration setting. With <> enabled, the name scope is global i.e. +there may not be two generator definitions with the same name. Historically, Hibernate ORM used a local scope i.e. +every managed type may have a generator with the same name, preferring the "local" definition over a more distant one. +This behavior allows to model e.g. a mapped superclass with a generator that should apply to subclasses by default, +yet allow an entity subclass to choose a custom generator by simply specifying the same generator name. + +[[identifiers-generators-sequence-override]] +.Sequence override +==== +[source,java] +---- +include::{example-dir-generated}/override/SequenceGeneratorOverrideTest.java[tag=identifiers-generators-sequence-override-example, indent=0] +---- +==== + +In this case, `base_sequence` will be used when a `Entity1` is persisted, +whereas for persists of a `Entity2`, Hibernate ORM will use `sub_sequence`. [[identifiers-generators-identity]] ==== Using IDENTITY columns