HHH-11069 - Improve documentation regarding GenerationType.AUTO

Explain the two IdGeneratorStrategyInterpreter implementations and their relation to how AUTO resolves to an actual identifier strategy
This commit is contained in:
Vlad Mihalcea 2016-08-29 17:29:41 +03:00
parent aaa32ebed5
commit d88bf908da
1 changed files with 12 additions and 8 deletions

View File

@ -221,17 +221,21 @@ The rest of the discussion here assumes this setting is enabled (true).
==== Interpreting AUTO
How a persistence provider interprets the AUTO generation type is left up to the provider.
Hibernate interprets it in the following order:
* If the given name matches the name for a `javax.persistence.SequenceGenerator` annotation then <<identifiers-generators-sequence>>.
* If the given name matches the name for a `javax.persistence.TableGenerator` annotation then <<identifiers-generators-table>>.
* If the given name matches the name for a `org.hibernate.annotations.GenericGenerator` annotation then <<identifiers-generators-generic>>.
The default behavior is to look at the java type of the identifier attribute.
The fallback is to consult with the pluggable `org.hibernate.boot.model.IdGeneratorStrategyInterpreter` contract, which is covered in detail in the Hibernate Integrations Guide.
The default behavior is to look at the java type of the identifier attribute:
If the identifier type is UUID, Hibernate is going to use an <<identifiers-generators-uuid, UUID identifier>>.
* for UUID <<identifiers-generators-uuid>>
* Otherwise, <<identifiers-generators-sequence>>
If the identifier type is numerical (e.g. `Long`, `Integer`), then Hibernate is going to use the `IdGeneratorStrategyInterpreter` to resolve the identifier generator strategy.
The `IdGeneratorStrategyInterpreter` has two implementations:
`FallbackInterpreter`::
This is the default strategy since Hibernate 5.0. For older versions, this strategy is enabled through the <<appendices/Configurations.adoc#configurations-mapping,`hibernate.id.new_generator_mappings`>> configuration property .
When using this strategy, `AUTO` always resolves to `SequenceStyleGenerator`.
If the underlying database supports sequences, then a SEQUENCE generator is used. Otherwise, a TABLE generator is going to be used instead.
`LegacyFallbackInterpreter`::
This is a legacy mechanism that was used by Hibernate prior to version 5.0 or when the <<appendices/Configurations.adoc#configurations-mapping,`hibernate.id.new_generator_mappings`>> configuration property is false.
The legacy strategy maps `AUTO` to the `native` generator strategy which uses the https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/dialect/Dialect.html#getNativeIdentifierGeneratorStrategy--[Dialect#getNativeIdentifierGeneratorStrategy] to resolve the actual identifier generator (e.g. `identity` or `sequence`).
[[identifiers-generators-sequence]]
==== Using sequences